Not getting full response body


(Mrk) #1

hi i want to send back the response body , currently i am getting only status back .

			jumbo_rod_url :="some url "
			body := strings.NewReader(string(ctx.Request.Body()))
			fmt.Println("printing body",body)
			req, err := http.NewRequest("POST", jumbo_rod_url,body)

			req.Header.Add("Authorization", "Bearer "+jumbo_token[2])

			req.Header.Add("Content-Type", "application/json")

			if err != nil {
				log.Panic(err)
			}

			resp, _ := http.DefaultClient.Do(req)
			defer resp.Body.Close()
			fmt.Fprintf(ctx,"%s",resp)
			fmt.Printf("%s","final response ",resp)

//response i am geting

                          // {200 OK %!s(int=200) HTTP/1.1 %!s(int=1) %!s(int=1) map[X-Content-//Type-Options:[nosniff] X-Xss-Protection:[1;mode=block] 
			fmt.Println("check",resp.Body)

//response i need to get as in swagger
///
{
“scope”: “/client”,
“id”: {
“member_id”: “”
},
“access_token”: “”,
“token_type”: “bearer”,
“expires_in”: 3600,
“refresh_token”: “”,
“refresh_expires_in”: 3600
}

		}

(Jakob Borg) #2

“resp” is not a string. See the examples at https://golang.org/pkg/net/http/


(Mrk) #3

hi Jakob,
how can i get the entire body of the response and not just that status ?


(Norbert Melzer) #4

You took a look at the examples @calmh linked in his post?

Its there, right in the second example.


(Sunny Karira) #5
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Read err")
}
fmt.Printf("%s","final response ",string(respBody))

this would work for string
now you would have to json.Unmarshal to get the data in a desired struct


(Mrk) #6

Thanks , i was able to resolve my issue :slight_smile:


(Sunny Karira) #7

Btw ioutil.ReadAll is not a good idea.
Check this


(system) #8

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