[{"data":1,"prerenderedAt":171},["Reactive",2],{"/guides/go/nethttp/avoid-conversion-on-error":3,"related:jnCiehGaan":89},{"_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":16,"body":17,"_type":84,"_id":85,"_source":86,"_file":87,"_extension":88},"/guides/go/nethttp/avoid-conversion-on-error","nethttp",false,"","Avoid the conversion on error when loading the document","Learn how to avoid the conversion on error when loading the document using Go and the Net/HTTP library and relies on the PDFShift's API.","Go","Net/HTTP","raise_for_status","pdf",[15],"convert-html-to-pdf-from-raw-html",true,{"type":18,"children":19,"toc":81},"root",[20,28,49,61],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":27},"text","In this guide, we'll show you how to abort the conversion when loading the distant source does not return a 2XX response.",{"type":21,"tag":22,"props":29,"children":30},{},[31,33,39,41,47],{"type":26,"value":32},"When you're converting a document, you might want to avoid the conversion on error when loading the document. This can be done by setting the ",{"type":21,"tag":34,"props":35,"children":37},"code",{"className":36},[],[38],{"type":26,"value":12},{"type":26,"value":40}," parameter to ",{"type":21,"tag":34,"props":42,"children":44},{"className":43},[],[45],{"type":26,"value":46},"True",{"type":26,"value":48}," in the request.",{"type":21,"tag":50,"props":51,"children":56},"pre",{"className":52,"code":54,"language":55,"meta":7},[53],"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    params := map[string]interface{}{\n        \"source\":            \"https://www.httpstat.us/404\",\n        \"raise_for_status\":  true,\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",[57],{"type":21,"tag":34,"props":58,"children":59},{"__ignoreMap":7},[60],{"type":26,"value":54},{"type":21,"tag":22,"props":62,"children":63},{},[64,66,71,73,79],{"type":26,"value":65},"Passing ",{"type":21,"tag":34,"props":67,"children":69},{"className":68},[],[70],{"type":26,"value":12},{"type":26,"value":72}," to ",{"type":21,"tag":34,"props":74,"children":76},{"className":75},[],[77],{"type":26,"value":78},"true",{"type":26,"value":80}," will ensure that if PDFShift can not load your document, the conversion will fail with an error.",{"title":7,"searchDepth":82,"depth":82,"links":83},2,[],"markdown","content:guides:go:nethttp:avoid-conversion-on-error.md","content","guides/go/nethttp/avoid-conversion-on-error.md","md",[90,94,98,102,106,107,111,115,119,123,127,131,135,139,143,147,151,155,159,163,167],{"_path":91,"title":92,"language":10,"library":11,"_id":93},"/guides/go/nethttp/accessing-secured-pages","Accessing secured pages","content:guides:go:nethttp:accessing-secured-pages.md",{"_path":95,"title":96,"language":10,"library":11,"_id":97},"/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":99,"title":100,"language":10,"library":11,"_id":101},"/guides/go/nethttp/adding-a-text-watermark","Adding a text watermark","content:guides:go:nethttp:adding-a-text-watermark.md",{"_path":103,"title":104,"language":10,"library":11,"_id":105},"/guides/go/nethttp/adding-an-image-watermark","Adding an image watermark","content:guides:go:nethttp:adding-an-image-watermark.md",{"_path":4,"title":8,"language":10,"library":11,"_id":85},{"_path":108,"title":109,"language":10,"library":11,"_id":110},"/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":112,"title":113,"language":10,"library":11,"_id":114},"/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":116,"title":117,"language":10,"library":11,"_id":118},"/guides/go/nethttp/define-a-time-limit","Define a time limit","content:guides:go:nethttp:define-a-time-limit.md",{"_path":120,"title":121,"language":10,"library":11,"_id":122},"/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":124,"title":125,"language":10,"library":11,"_id":126},"/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":128,"title":129,"language":10,"library":11,"_id":130},"/guides/go/nethttp/loading-css-from-a-string","Loading CSS from a string","content:guides:go:nethttp:loading-css-from-a-string.md",{"_path":132,"title":133,"language":10,"library":11,"_id":134},"/guides/go/nethttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:go:nethttp:loading-css-from-a-url.md",{"_path":136,"title":137,"language":10,"library":11,"_id":138},"/guides/go/nethttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:go:nethttp:loading-javascript-from-a-string.md",{"_path":140,"title":141,"language":10,"library":11,"_id":142},"/guides/go/nethttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:go:nethttp:loading-javascript-from-a-url.md",{"_path":144,"title":145,"language":10,"library":11,"_id":146},"/guides/go/nethttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:go:nethttp:protecting-the-generated-pdf.md",{"_path":148,"title":149,"language":10,"library":11,"_id":150},"/guides/go/nethttp/receive-a-webhook-event","Receive a webhook event","content:guides:go:nethttp:receive-a-webhook-event.md",{"_path":152,"title":153,"language":10,"library":11,"_id":154},"/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":156,"title":157,"language":10,"library":11,"_id":158},"/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":160,"title":161,"language":10,"library":11,"_id":162},"/guides/go/nethttp/send-custom-http-headers","Send custom HTTP headers","content:guides:go:nethttp:send-custom-http-headers.md",{"_path":164,"title":165,"language":10,"library":11,"_id":166},"/guides/go/nethttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:go:nethttp:using-cookies.md",{"_path":168,"title":169,"language":10,"library":11,"_id":170},"/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",1785426824573]