anonrose
anonrose

Reputation: 1381

How to make a get request mid application

How would I go about creating a GET HTTP request mid application in GAE? I don't want to have it as a handler function, I simply have a URL that I need to get the response body from.

Upvotes: 0

Views: 88

Answers (2)

Thundercat
Thundercat

Reputation: 121119

Use the urlfetch package.

ctx := appengine.NewContext(r)   // r is the *http.Request arg to the handler
client := urlfetch.Client(ctx)  
resp, err := client.Get("http://example.com")
if err != nil {
    // handle the error 
}
body := resp.Body // body is an io.Reader containing the response body

Here's a complete example.

Upvotes: 1

Endophage
Endophage

Reputation: 21483

I haven't used it so apologies if this doesn't work. According to GAE's docs you probably want to use urlfetch to get a *http.Client something like (N.B. the context package is standard in the just released Go 1.7):

import (
    "context" // Go 1.7
    // "golang.org/x/net/context" // Go < 1.7
    "google.golang.org/appengine/urlfetch"
)

client := urlfetch.Client(context.Background())
resp, err := client.Get("http://example.com/")

Upvotes: 0

Related Questions