Payload variable in Go examples

Perhaps I missed it in the documentation - what is intended to be in the payload variable in the Go examples?

Like this:
req, _ := http.NewRequest(“GET”, url, payload)

You are referring to a specific endpoint or in general?

There are many endpoints that have it referenced, but in this case I’m looking at this one

It looks like a typo in documentation as that variable wasn’t declared before.

payload is required for the call but I don’t know what’s expected to be in it

Did you try with an empty value?

Yes, that didn’t work.

You can check the example with curl and make an equivalent in go assuming that you know go better than the other programming languages form the documentation.

I think what’s there is the equivalent in Go, it’s just missing some doco. Hmmmm.

This is where I’m currently stuck - not sure what’s wrong

package main

import (
“fmt”
“io/ioutil”
“net/http”
“strings”
)

func main() {

url := "https://deep-index.moralis.io/api/v2/nft/0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0/trades?chain=eth&marketplace=opensea"
payload := getPayload()
req, _ := http.NewRequest("GET", url, payload)

req.Header.Add("Accept", "application/json")
req.Header.Add("X-API-Key", "MyAPIKey")

res, err := http.DefaultClient.Do(req)
if err != nil {
	fmt.Println("Error executing request:", err)
	return
}

defer func() {
	if err := res.Body.Close(); err != nil {
		fmt.Println("Error closing response body:", err)
	}
}()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
	fmt.Println("Error reading response body:", err)
	return
}

fmt.Println(res)
fmt.Println(string(body))

}

func getPayload() *strings.Reader {
payload := strings.NewReader({"from_block":1,"to_block":9999999999,"limit":500,"cursor":"","disable_total":true})
return payload
}

What happens? What doesn’t work?

I actually rewrote the function entirely and it works - I am having some issues getting the cursor part to work now

1 Like