View all our code samples

Using go-resty in Go

Go-Resty is a third-party HTTP client library for GoLang. It provides an easy-to-use API for making HTTP requests and handling responses, similar to libraries like net/http. Go-Resty simplifies tasks such as setting headers, handling cookies, and managing request parameters.

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 (
    "encoding/json"
    "fmt"
    "github.com/go-resty/resty/v2"
    "io/ioutil"
)

// Convert function makes a POST request to the provided endpoint with the supplied parameters
func convert(apiKey string, params map[string]string, endpoint string) ([]byte, error) {
    client := resty.New()

    resp, err := client.R().
        SetBasicAuth("api", apiKey).
        SetHeader("Content-Type", "application/json").
        SetBody(params).
        Post(fmt.Sprintf("https://api.pdfshift.io/v3/convert/%s", endpoint))

    if err != nil {
        return nil, err
    }

    if _, ok := params["filename"]; ok || params["webhook"]; ok {
        var response map[string]interface{}
        err := json.Unmarshal(resp.Body(), &response)
        if err != nil {
            return nil, err
        }
        return json.Marshal(response)
    }

    return resp.Body(), nil
}

Here's how you can use the above code:

func main() {
    params := map[string]string{"source": "https://en.wikipedia.org/wiki/REST"}
    apiKey := "sk_XXXXXXXXXXXXXX"

    resp, err := convert(apiKey, params, "pdf")
    if err != nil {
        panic(err)
    }

    error := ioutil.WriteFile("result.pdf", resp, 0644)
    if error != nil {
        panic(err)
    }
}

We've tested this code with the latest version of go-resty 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.