I'm building the API with golang. I want this endpoint to return json data so I can use it in my frontend.
http.HandleFunc("/api/orders", createOrder)
Currently my function is not returning a json object and jsonMap variable is not maping the response body to of the server with the Create struc
My struct
type CreateOrder struct {
Id string `json:"id"`
Status string `json:"status"`
Links []Links `json:"links"`
}
My CreateOrder function (updated based on comments)
func createOrder(w http.ResponseWriter, r *http.Request) {
accessToken := generateAccessToken()
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Println(accessToken)
body := []byte(`{
"intent":"CAPTURE",
"purchase_units":[
{
"amount":{
"currency_code":"USD",
"value":"100.00"
}
}
]
}`)
req, err := http.NewRequest("POST", base+"/v2/checkout/orders", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
fmt.Println(resp.StatusCode)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
var jsonMap CreateOrder
error := json.NewDecoder(resp.Body).Decode(&jsonMap)
if error != nil {
log.Fatal(err)
}
w.WriteHeader(resp.StatusCode)
json.NewEncoder(w).Encode(jsonMap)
}
This is what gets printed. Prints the value without the keys of the object
{2MH36251C2958825N CREATED [{something self GET} {soemthing approve GET}]}
Should print
{
id: '8BW01204PU5017303',
status: 'CREATED',
links: [
{
href: 'url here',
rel: 'self',
method: 'GET'
},
...
]
}
json.NewDecoder().Decodeis for decoding the response you got from the external service. To then pass the data along to the frontend you still need to do thejson.NewEncoder().Encode. And if you want to pass the data from the external service to the frontend verbatim, i.e. without modification, you can skip the decode/encode steps and just stream the data as is.