Golang

Here is an example of how to make a request in Go

package main

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

func createRequest() {
	apiUrl := "http://127.0.0.1:8080/api/v4/flclient"

	payloadJSON := map[string]interface{}{
		"proxies": map[string]interface{}{
			"useproxy": false,
		},
		"url": "https://api.myip.com/",
		"method": "GET",
		"headers": map[string]interface{}{},
		"client": 1,
		"protocol": "HTTP/2.0",
		"redirect": false,
		"timeout": 10,

	}

	jsonData, err := json.Marshal(payloadJSON)

	if err != nil {
		return
	}

	request, error1 := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonData))
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Authorization", "Bearer xxxx-xxxx")

	client := &http.Client{}
	response, error := client.Do(request)

	if error1 != nil {
		return
	}

	if error != nil {
		return
	}

	body, error := io.ReadAll(response.Body)

	if error != nil {
		return
	}

	formattedData := formatJSON(body)
	fmt.Println("Response: ", formattedData)
}

func formatJSON(data []byte) string {
    var out bytes.Buffer
    err := json.Indent(&out, data, "", " ")

    if err != nil {
        fmt.Println(err)
    }

    d := out.Bytes()
    return string(d)
}

func main() {
	createRequest()
}

Last updated