Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created April 10, 2016 23:41
Show Gist options
  • Save linxlad/4b15e215412b53672bf7097c31accf42 to your computer and use it in GitHub Desktop.
Save linxlad/4b15e215412b53672bf7097c31accf42 to your computer and use it in GitHub Desktop.
Test Go Http Request
package main
import (
// "bytes"
. "fmt"
"net/http"
// "io/ioutil"
// "net/url"
// "strconv"
"net/url"
//"bytes"
"io"
"strings"
)
func requestBroker(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get("username")
if len(username) == 0 {
Fprintf(w, "Github username required.")
return
}
apiUrl := "https://api.github.com"
path := "/users/"
s := []string{}
s = append(s, path)
s = append(s, username)
resource := strings.Join(s, "")
u, _ := url.ParseRequestURI(apiUrl)
u.Path = resource
urlStr := Sprintf("%v", u) // "https://api.com/user/"
client := &http.Client{}
// var jsonStr = []byte(`{"title":"Buy cheese and bread for breakfast."}`)
req, _ := http.NewRequest("GET", urlStr, nil)
// w.Header().Set("Content-Disposition", "attachment; filename=WHATEVER_YOU_WANT")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
resp, _ := client.Do(req)
io.Copy(w, resp.Body)
}
func main() {
http.HandleFunc("/", requestBroker)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment