I need to send Body which is in json to api call

gid_token := r.Header.Get("Authorization")

		request_url_gid := r.RequestURI
		jmb_gid_url := gid_env_url + request_url_gid

		body :=r.Body
		fmt.Print("%T",body)
		c_type :=r.Header.Get("Content-Type")
		fmt.Println("c_type",c_type)
		req, _ := http.NewRequest("POST", jmb_gid_url,body)
		req.Header.Add("Authorization","xxxxxx")
		req.Header.Add("Content-Type","application/json")
		fmt.Println(req)
		resp, _ := http.DefaultClient.Do(req)
		defer resp.Body.Close()
		bodynew, _ := ioutil.ReadAll(resp.Body)
		fmt.Fprintf(w, "%s", bodynew)

}

Please edit your post and indent every line of code with four spaces. The </> button will help you with that.

sorry about that .

No problem.

Regarding your question: You’ve just posted a snippet of code but you do not tell us what this code is supposed to do, if it is doing what you want it to do, or if there is any problem or error. What is your question?

i want to send Json Body but that is not going as part of the request

	// q is your struct
    // url is your url.URL

body := &bytes.Buffer{}
if err := json.NewEncoder(body).Encode(&q); err != nil {
	panic(err)
}

req, err := http.NewRequest(http.MethodPost, url.String(), body)

thanks :slight_smile:
My version : req, err := http.NewRequest(“POST”, url, strings.NewReader(r.Form.Encode()))

hi i have 1 question . Even if the values r empty how can i send only the fields to the api

Use struct with json annotations.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.