Http Response Body parsing

17,532 views
Skip to first unread message

ThePiachu

unread,
Sep 8, 2011, 2:36:04 PM9/8/11
to golang-nuts
I`ve been trying to find some way to parse a simple http response into
a string, but I`m having problems with this for hours now.
I copied the tutorial, and changed the url into something I want to be
getting. The code looks like this:

func hello(w http.ResponseWriter, r *http.Request) {

c := appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.Get("http://blockexplorer.com/q/
getdifficulty")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
//resp.Write(w)
fmt.Fprintf(w, "%s", resp.Body)
}

It prints:
&{1777774.4820015 %!s(bool=false) %!s(bool=false)}
and should print:
1777774.4820015

if I use resp.Write(w) it prints:
HTTP/1.1 200 200 OK Connection: close Content-Length: 15 Cache-
Control: no-cache Content-Type: text/plain Date: Thu, 08 Sep 2011
18:33:44 GMT Server: Apache/2.2.14 (Ubuntu) Vary: Accept-Encoding X-
Powered-By: PHP/5.3.2-1ubuntu4.9 1777774.4820015

which is too much, but the end of the string looks better. Can't seem
to call anything on resp.Body, or get any other access to the data. Am
I missing some obvious way to parse the data, or am I doing something
wrong?

Thanks in advance for any solutions.

Brad Fitzpatrick

unread,
Sep 8, 2011, 2:46:44 PM9/8/11
to ThePiachu, golang-nuts
resp.Body is an io.ReadCloser, so you can call Read or Close on it.

bs, err := ioutil.ReadAll(resp.Body)
if err {
   ...
}
s
tr := string(bs)

barnex

unread,
Sep 8, 2011, 3:47:25 PM9/8/11
to golang-nuts
var client http.Client
resp, err := client.Get(url)
defer resp.Body.Close()
if err != nil {
...
}


if resp.StatusCode == 200 { // OK
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
bodyString = string(bodyBytes)
}

ThePiachu

unread,
Sep 8, 2011, 3:39:32 PM9/8/11
to golang-nuts
Thank you, it works now.

Andrew Chilton

unread,
Sep 9, 2011, 6:38:07 PM9/9/11
to barnex, golang-nuts
On 9 September 2011 07:47, barnex <a.vanst...@gmail.com> wrote:
>    var client http.Client
>    resp, err := client.Get(url)
>    defer resp.Body.Close()
>    if err != nil {
>        ...
>    }

Just as a side note, you've got a mistake that has got me before too.
You need to check the error before doing the deferred close. :)

var client http.Client
resp, err := client.Get(url)

if err != nil {
   ...
}

defer resp.Body.Close()

If there has been an error, then there is no Body to close so you must
check first.

Cheers,
Andy

--
Andrew Chilton
e: chi...@appsattic.com
w: http://www.appsattic.com/

asadov...@gmail.com

unread,
May 1, 2012, 2:21:46 AM5/1/12
to golan...@googlegroups.com, barnex


On Friday, September 9, 2011 3:38:07 PM UTC-7, Andrew Chilton wrote:
On 9 September 2011 07:47, barnex <a.vanst...@gmail.com> wrote:
>    var client http.Client
>    resp, err := client.Get(url)
>    defer resp.Body.Close()
>    if err != nil {
>        ...
>    }

Just as a side note, you've got a mistake that has got me before too.
You need to check the error before doing the deferred close. :)

var client http.Client
resp, err := client.Get(url)
if err != nil {
    ...
}
defer resp.Body.Close()

If there has been an error, then there is no Body to close so you must
check first.


Just came across this while searching. How do we know for sure that there's no Body in this case?
Isn't it possible that there was a non-fatal error, so err != nil but resp has a Body?

In particular, http://golang.org/pkg/net/http/#Client.Do says that Body is non-nil if resp is non-nil, but says nothing about the error case.
So it seems we should write this instead:

resp, err := client.Do(request)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}

Maybe I'm missing something? (I'm new to Go.)

rahul....@gmail.com

unread,
Sep 15, 2016, 11:08:44 AM9/15/16
to golang-nuts
Anybody knows about parsing data which is coming in somewhat json format? string/int is fine. What will be the best way to parse json response.

Shawn Milochik

unread,
Sep 15, 2016, 11:50:14 AM9/15/16
to golang-nuts
Read the request.Body, close the request.Body, use the bytes read from request.Body in json.Unmarshal.
Reply all
Reply to author
Forward
0 new messages