I’ve the under code that fetch some date from server, and put the info right into a struct.
The info is fetched appropriately, however I’m getting err: EOF
as an error whereas decoding the returned information into struct:
bundle major
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"internet/http"
)
kind 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 major() {
req, _ := http.NewRequest("GET", "https://script.google.com/macros/s/AKfycbzw0TKWycxeB5sx1wIefAiEHeYQt2mVuM-NAZTccxedhyntdv8FvcUteOZ2k03wRHGE/exec?", nil)
q := req.URL.Question()
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 Learn the response physique on the road under.
physique, err := ioutil.ReadAll(resp.Physique)
if err != nil {
log.Fatalln(err)
}
//Convert the physique to kind string
sb := string(physique)
log.Printf(sb)
var goal = new(SKUcard)
err = json.NewDecoder(resp.Physique).Decode(goal)
if err != nil {
fmt.Println("err:", err)
}
log.Printf(goal.BarCode)
}
From what I see you learn twice from the Physique
ioutil.ReadAll(resp.Physique)
json.NewDecoder(resp.Physique)
since Physique is a ReadCloser
IMHO the second time you’ll discover it already learn.
So, both:
- take away the primary learn
ioutil.ReadAll
- use the json decoder on a learn buffer initialized to
physique
buf := bytes.NewBuffer(physique)
err = json.NewDecode(buf).Decode(goal)
as a facet notice I at all times counsel to examine the StatusCode
of the response earlier than do physique processing
1 Like