[{"data":1,"prerenderedAt":149},["Reactive",2],{"/guides/go/nethttp/accessing-secured-pages":3,"related:wdiQ9YcRwD":67},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":17,"body":18,"_type":62,"_id":63,"_source":64,"_file":65,"_extension":66},"/guides/go/nethttp/accessing-secured-pages","nethttp",false,"","Accessing secured pages","Learn how to access secured pages using Go and the Net/HTTP library. This guide offers detailed steps with code samples in Go and the Net/HTTP library, highlighting how you can acces page protected by basic authentication to convert them to PDF using PDFShift's API.","Go","Net/HTTP","auth","pdf",[15,16],"send-custom-http-headers","using-cookies",true,{"type":19,"children":20,"toc":59},"root",[21,29,42,54],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"text","In this guide, we'll show you how to access secured page (protected by basic authentication) using Go and the Net/HTTP library to convert them to PDF using PDFShift's API.",{"type":22,"tag":23,"props":30,"children":31},{},[32,34,40],{"type":27,"value":33},"When you're converting a document, you might want to access a secured page (protected by basic authentication) to convert it to PDF. This can be done by setting the ",{"type":22,"tag":35,"props":36,"children":38},"code",{"className":37},[],[39],{"type":27,"value":12},{"type":27,"value":41}," parameter to the request.",{"type":22,"tag":43,"props":44,"children":49},"pre",{"className":45,"code":47,"language":48,"meta":7},[46],"language-go","package main\n\nimport (\n    \"bytes\"\n    \"encoding/json\"\n    \"fmt\"\n    \"io/ioutil\"\n    \"net/http\"\n)\n\nfunc main() {\n    // You can get an API key at https://pdfshift.io\n    apiKey := \"sk_xxxxxxxxxxxx\"\n\n    // You can set a basic authentication by passing the \"auth\" property which contains a username and password\n    params := map[string]interface{}{\n        \"source\": \"https://www.example.com\",\n        \"auth\": map[string]string{\n            \"username\": \"user\",\n            \"password\": \"password\",\n        },\n    }\n\n    // Marshal the parameters into JSON\n    jsonParams, err := json.Marshal(params)\n    if err != nil {\n        fmt.Println(\"Error marshaling JSON:\", err)\n        return\n    }\n\n    // Create a new HTTP client\n    client := &http.Client{}\n\n    // Create a new request\n    req, err := http.NewRequest(\"POST\", \"https://api.pdfshift.io/v3/convert/pdf\", bytes.NewBuffer(jsonParams))\n    if err != nil {\n        fmt.Println(\"Error creating request:\", err)\n        return\n    }\n\n    // Set request headers\n    req.Header.Set(\"Content-Type\", \"application/json\")\n\n    // Set basic authentication header\n    req.Header.Set(\"X-API-Key\", apiKey)\n\n    // Perform the request\n    resp, err := client.Do(req)\n    if err != nil {\n        fmt.Println(\"Error performing request:\", err)\n        return\n    }\n    defer resp.Body.Close()\n\n    // Read response body\n    body, err := ioutil.ReadAll(resp.Body)\n    if err != nil {\n        fmt.Println(\"Error reading response body:\", err)\n        return\n    }\n\n    // Check response status code\n    if resp.StatusCode >= 400 {\n        fmt.Printf(\"Request failed with status code %d: %s\\n\", resp.StatusCode, string(body))\n        return\n    }\n\n    // Save the PDF document\n    err = ioutil.WriteFile(\"result.pdf\", body, 0644)\n    if err != nil {\n        fmt.Println(\"Error saving PDF document:\", err)\n        return\n    }\n\n    fmt.Println(\"The PDF document was generated and saved to result.pdf\")\n}\n","go",[50],{"type":22,"tag":35,"props":51,"children":52},{"__ignoreMap":7},[53],{"type":27,"value":47},{"type":22,"tag":23,"props":55,"children":56},{},[57],{"type":27,"value":58},"This allows you to protect your documents from any visitors while allowing PDFShift to access the page and convert it to PDF.",{"title":7,"searchDepth":60,"depth":60,"links":61},2,[],"markdown","content:guides:go:nethttp:accessing-secured-pages.md","content","guides/go/nethttp/accessing-secured-pages.md","md",[68,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,129,133,137,141,145],{"_path":4,"title":8,"language":10,"library":11,"_id":63},{"_path":70,"title":71,"language":10,"library":11,"_id":72},"/guides/go/nethttp/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:go:nethttp:adding-a-custom-header-or-footer.md",{"_path":74,"title":75,"language":10,"library":11,"_id":76},"/guides/go/nethttp/adding-a-text-watermark","Adding a text watermark","content:guides:go:nethttp:adding-a-text-watermark.md",{"_path":78,"title":79,"language":10,"library":11,"_id":80},"/guides/go/nethttp/adding-an-image-watermark","Adding an image watermark","content:guides:go:nethttp:adding-an-image-watermark.md",{"_path":82,"title":83,"language":10,"library":11,"_id":84},"/guides/go/nethttp/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:go:nethttp:avoid-conversion-on-error.md",{"_path":86,"title":87,"language":10,"library":11,"_id":88},"/guides/go/nethttp/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:go:nethttp:convert-html-to-pdf-from-a-url.md",{"_path":90,"title":91,"language":10,"library":11,"_id":92},"/guides/go/nethttp/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:go:nethttp:convert-html-to-pdf-from-raw-html.md",{"_path":94,"title":95,"language":10,"library":11,"_id":96},"/guides/go/nethttp/define-a-time-limit","Define a time limit","content:guides:go:nethttp:define-a-time-limit.md",{"_path":98,"title":99,"language":10,"library":11,"_id":100},"/guides/go/nethttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:go:nethttp:exporting-only-a-specific-set-of-pages.md",{"_path":102,"title":103,"language":10,"library":11,"_id":104},"/guides/go/nethttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:go:nethttp:generate-a-document-with-full-height.md",{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/go/nethttp/loading-css-from-a-string","Loading CSS from a string","content:guides:go:nethttp:loading-css-from-a-string.md",{"_path":110,"title":111,"language":10,"library":11,"_id":112},"/guides/go/nethttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:go:nethttp:loading-css-from-a-url.md",{"_path":114,"title":115,"language":10,"library":11,"_id":116},"/guides/go/nethttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:go:nethttp:loading-javascript-from-a-string.md",{"_path":118,"title":119,"language":10,"library":11,"_id":120},"/guides/go/nethttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:go:nethttp:loading-javascript-from-a-url.md",{"_path":122,"title":123,"language":10,"library":11,"_id":124},"/guides/go/nethttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:go:nethttp:protecting-the-generated-pdf.md",{"_path":126,"title":127,"language":10,"library":11,"_id":128},"/guides/go/nethttp/receive-a-webhook-event","Receive a webhook event","content:guides:go:nethttp:receive-a-webhook-event.md",{"_path":130,"title":131,"language":10,"library":11,"_id":132},"/guides/go/nethttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:go:nethttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/guides/go/nethttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:go:nethttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/go/nethttp/send-custom-http-headers","Send custom HTTP headers","content:guides:go:nethttp:send-custom-http-headers.md",{"_path":142,"title":143,"language":10,"library":11,"_id":144},"/guides/go/nethttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:go:nethttp:using-cookies.md",{"_path":146,"title":147,"language":10,"library":11,"_id":148},"/guides/go/nethttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:go:nethttp:waiting-for-a-custom-element-to-be-ready.md",1785426824549]