[{"data":1,"prerenderedAt":66},["Reactive",2],{"/samples/go/nethttp":3,"related:IVaWBQwU6y":60},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"link":12,"body":13,"_type":55,"_id":56,"_source":57,"_file":58,"_extension":59},"/samples/go/nethttp","go",false,"","Nethttp","Net/HTTP is a core package that provides functionality for building HTTP servers and clients, handling HTTP requests and responses, and managing web communication. It offers a straightforward API for creating web applications, APIs, and microservices, making it a fundamental component of web development in Go.","Go","Net/HTTP","https://golang.org/pkg/net/http/",{"type":14,"children":15,"toc":52},"root",[16,31,43],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21,29],{"type":17,"tag":22,"props":23,"children":26},"a",{"href":12,"rel":24},[25],"nofollow",[27],{"type":28,"value":11},"text",{"type":28,"value":30}," is a core package that provides functionality for building HTTP servers and clients, handling HTTP requests and responses, and managing web communication. It offers a straightforward API for creating web applications, APIs, and microservices, making it a fundamental component of web development in Go.",{"type":17,"tag":32,"props":33,"children":37},"pre",{"className":34,"code":36,"language":5,"meta":7},[35],"language-go","package main\n\nimport (\n    \"bytes\"\n    \"encoding/json\"\n    \"io/ioutil\"\n    \"net/http\"\n)\n\ntype Response struct {\n    Filename string `json:\"filename\"`\n    Webhook  string `json:\"webhook\"`\n    Status   string `json:\"status\"`\n}\n\nfunc convert(apiKey string, params map[string]interface{}, endpoint string) ([]byte, error) {\n    allowedEndpoints := map[string]bool{\n        \"pdf\":  true,\n        \"png\":  true,\n        \"jpg\":  true,\n        \"webp\": true,\n    }\n    \n    if _, ok := allowedEndpoints[endpoint]; !ok {\n        panic(\"Invalid endpoint! Allowed endpoints are 'pdf', 'png', 'jpg', 'webp'\")\n    }\n    \n    url := \"https://api.pdfshift.io/v3/convert/\" + endpoint\n    jsonParams, err := json.Marshal(params)\n    if err != nil {\n        return nil, err\n    }\n\n    req, err := http.NewRequest(\"POST\", url, bytes.NewBuffer(jsonParams))\n    if err != nil {\n        return nil, err\n    }\n    req.Header.Set(\"Content-Type\", \"application/json\")\n    req.Header.Set(\"X-API-Key\", apiKey)\n\n    client := &http.Client{}\n    resp, err := client.Do(req)\n    if err != nil {\n        return nil, err\n    }\n    defer resp.Body.Close()\n\n    if _, ok := params[\"filename\"]; ok || params[\"webhook\"]; ok {\n        responseData := &Response{}\n        decoder := json.NewDecoder(resp.Body)\n        err := decoder.Decode(&responseData)\n        if err != nil {\n            return nil, err\n        }\n        jsonResponse, err := json.Marshal(responseData)\n        if err != nil {\n            return nil, err\n        }\n        return jsonResponse, nil\n    }\n\n    body, err := ioutil.ReadAll(resp.Body)\n    if err != nil {\n        return nil, err\n    }\n    return body, nil\n}\n",[38],{"type":17,"tag":39,"props":40,"children":41},"code",{"__ignoreMap":7},[42],{"type":28,"value":36},{"type":17,"tag":32,"props":44,"children":47},{"className":45,"code":46,"language":5,"meta":7},[35],"func main() {\n    params := map[string]interface{}{\n        \"source\": \"https://en.wikipedia.org/wiki/REST\",\n    }\n    data, err := convert(\"sk_XXXXXXXXXXXXXX\", params, \"pdf\")\n    if err != nil {\n        panic(err)\n    }\n    err = ioutil.WriteFile(\"result.pdf\", data, 0644)\n    if err != nil {\n        panic(err)\n    }\n}\n",[48],{"type":17,"tag":39,"props":49,"children":50},{"__ignoreMap":7},[51],{"type":28,"value":46},{"title":7,"searchDepth":53,"depth":53,"links":54},2,[],"markdown","content:samples:go:nethttp.md","content","samples/go/nethttp.md","md",[61,65],{"_path":62,"language":10,"library":63,"_id":64},"/samples/go/go-resty","go-resty","content:samples:go:go-resty.md",{"_path":4,"language":10,"library":11,"_id":56},1785426823217]