Reading Http request body not getting payload at this scenario

HttpServer.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var response string
if validHTTPRequest(r, &response) {

## _fmt.Println(r.body) it s not working not getting payload

RequestAbstract := mapHTTPAbstractRequest®

		resp, _ := HandleRoutes(RequestAbstract)

		json.Unmarshal([]byte(resp.Data), &resp.DataHTTP)

		resp.Data = ""
		response = utils.EncodeJSON(resp)
	}

}

func validHTTPRequest(r *http.Request, response *string) bool{

body, err := ioutil.ReadAll(r.Body)

	if !json.unmarshal(body) {
	
	}

fmt.Println(string(body)) it is fine

}

In the handler you are trying to print r.Body, in validHTTPRequest you print body, which is the result of reading r.Body.

Please indent your code using the </> button, it is very hard to read.

request payload data

Perhaps I should approach this differently:

  1. What do you expect to happen?
  2. What actually happens?

it will come nil

in validHttpRequest is validate for json correct are not…affter validation then if we printf r.Body not getting data

Affter got request with payload first i have validate is it correct json are not then if print r.Body it will getting empty befoure i writte r.Body without validation it work fine

The validation routine reads everything from r.Body. After ioutil.ReadAll has completed, r.Body is empty (because everything was read from it).

You don’t need to validate the JSON separately. Unmarshal it directly to wherever it needs to go. If there is a problem, json.Unmarshal will return an error that you can then handle.

1 Like

Okay Thank you

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