View all our code samples

Using Net/HTTP in Go

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.

We've implemented a code sample that you can re-use to convert your HTML documents to PDF, JPG, PNG or WEBP using PDFShift and Go:

package main

import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "net/http"
)

type Response struct {
    Filename string `json:"filename"`
    Webhook  string `json:"webhook"`
    Status   string `json:"status"`
}

func convert(apiKey string, params map[string]interface{}, endpoint string) ([]byte, error) {
    allowedEndpoints := map[string]bool{
        "pdf":  true,
        "png":  true,
        "jpg":  true,
        "webp": true,
    }
    
    if _, ok := allowedEndpoints[endpoint]; !ok {
        panic("Invalid endpoint! Allowed endpoints are 'pdf', 'png', 'jpg', 'webp'")
    }
    
    url := "https://api.pdfshift.io/v3/convert/" + endpoint
    jsonParams, err := json.Marshal(params)
    if err != nil {
        return nil, err
    }

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonParams))
    if err != nil {
        return nil, err
    }
    req.Header.Set("Content-Type", "application/json")
    req.SetBasicAuth("api", apiKey)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if _, ok := params["filename"]; ok || params["webhook"]; ok {
        responseData := &Response{}
        decoder := json.NewDecoder(resp.Body)
        err := decoder.Decode(&responseData)
        if err != nil {
            return nil, err
        }
        jsonResponse, err := json.Marshal(responseData)
        if err != nil {
            return nil, err
        }
        return jsonResponse, nil
    }

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }
    return body, nil
}

Here's how you can use the above code:

func main() {
    params := map[string]interface{}{
        "source": "https://en.wikipedia.org/wiki/REST",
    }
    data, err := convert("sk_XXXXXXXXXXXXXX", params, "pdf")
    if err != nil {
        panic(err)
    }
    err = ioutil.WriteFile("result.pdf", data, 0644)
    if err != nil {
        panic(err)
    }
}

We've tested this code with the latest version of Net/HTTP and it's ready to be used in your project.

But if you were to encounter any bugs or issues while running it (or if you want to suggest changes to improve the code), please contact us and we'll be happy to help you out.