I’ve the below code that fetch some date from server, and put the data into a struct.
The data is fetched correctly, but I’m getting err: EOF
as an error while decoding the returned data into struct:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type SKUcard struct {
BarCode, SKUCode, VendorCode, RegistrationDate, VendorName, BrandName, ContactPerson,
ContactNumber, ItemName, ItemImage, NetWeight, CartoonPack, StorageTemperature, ShelfLife,
ShelfPrice, KottofCost, SupplyType, CoveredAreas, MinimumOrderQty, ContractDate, ReturnPolicy, Notes, InActive string
}
func main() {
req, _ := http.NewRequest("GET", "https://script.google.com/macros/s/AKfycbzw0TKWycxeB5sx1wIefAiEHeYQt2mVuM-NAZTccxedhyntdv8FvcUteOZ2k03wRHGE/exec?", nil)
q := req.URL.Query()
q.Add("barcode", "6287029390129")
//q.Add("another_thing", "foo & bar")
req.URL.RawQuery = q.Encode()
fmt.Println(req.URL.String())
// resp, err := http.Get(req.URL.String())
resp, err := http.DefaultClient.Do(req)
_ = req.URL.RawQuery
if err != nil {
log.Fatalln(err)
}
//We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
//Convert the body to type string
sb := string(body)
log.Printf(sb)
var target = new(SKUcard)
err = json.NewDecoder(resp.Body).Decode(target)
if err != nil {
fmt.Println("err:", err)
}
log.Printf(target.BarCode)
}