Why getting err: EOF while decoding responce body into struct

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 (

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()


	// resp, err := http.Get(req.URL.String())
	resp, err := http.DefaultClient.Do(req)
	_ = req.URL.RawQuery
	if err != nil {

	//We Read the response body on the line below.
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
	//Convert the body to type string
	sb := string(body)

	var target = new(SKUcard)
	err = json.NewDecoder(resp.Body).Decode(target)
	if err != nil {
		fmt.Println("err:", err)

From what I see you read twice from the Body

  • ioutil.ReadAll(resp.Body)
  • json.NewDecoder(resp.Body)

since Body is a ReadCloser IMHO the second time you’ll find it already read.

So, either:

  • remove the first read ioutil.ReadAll
  • use the json decoder on a read buffer initialized to body
buf := bytes.NewBuffer(body)
err = json.NewDecode(buf).Decode(target)

as a side note I always suggest to check the StatusCode of the response before do body processing