Simplified HTTP Client for Go.
HttpClient is designed to be the simplest way possible to make http requests. It sends an HTTP request and unmarshals json, xml, csv from the response in just one function call.
package main
import (
"fmt"
"log"
"github.com/tamnd/httpclient"
)
type Person struct {
ID string
FirstName string
Gender string
LastName string
Link string
Locate string
Name string
Username string
}
func main() {
var mark Person
err := httpclient.JSON("http://graph.facebook.com/4", &mark)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", mark)
}
Output:
main.Person{ID:"4", FirstName:"", Gender:"male", LastName:"", Link:"https://www.facebook.com/zuck", Locate:"", Name:"Mark Zuckerberg", Username:"zuck"}
Because I'm tired of typing the following code again and again:
resp, err := http.Get("http://api.example.com")
if err != nil {
return nil, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var data ...
err := decoder.Decode(&data)
if err != nil {
return nil, err
}
return &data, nil
It's better to wrap all the above code in a function and call it when you need send GET request and parse the response as JSON.
- Just use this package when you need to make some very simple HTTP GET requests then unmarshal the response body as json, xml, etc. If you need more than that, just use
net/http
, it is an excellent package, and has all things you need. - The better way is think that this package is just a collection of some code snippets. Feel free to open client.go and copy/paste just the code you need.
- Get and unmarshal JSON from a url
- Get and unmarshal XML from a url
- Download multipe files concurrency
$ go get github.com/tamnd/httpclient
import "github.com/tamnd/httpclient"
- Get String
- Get Bytes
- Get JSON
- Get XML
- Get Reader
- Download Files
- Send POST Request
- Upload Files
- Custom Request Header
func String(url string) (string, error)
String fetches the specified URL and returns the response body as a string.
content, err := httpclient.String("http://www.example.com")
func Bytes(url string) ([]byte, error)
Bytes fetches the specified url and returns the response body as bytes.
bytes, err := httpclient.Bytes("http://www.example.com")
func JSON(url string, v interface{}) error
JSON issues a GET request to a specified URL and unmarshal json data from the response body.
var user = struct {
ID string `json:"id"`
Gender string `json:"gender"`
Link string `json:"link"`
Name string `json:"name"`
Username string `json:"username"`
}{}
err := httpclient.JSON("http://graph.facebook.com/4", &user)
func Download(urls []string, files *[]File) error
Download downloads multiple files concurrency.
urls := []string{
"http://www.golang.org",
"http://www.clojure.org",
"http://www.erlang.org",
}
var files *[]httpclient.File
err := httpclient.Download(urls, files)
func Reader(url string) (io.ReadCloser, error)
Reader issues a GET request to a specified URL and returns an reader from the response body.
- Send POST request
- Custom request header
- Send basic authentication
- Make
Upload()
function - Get response header
- Connection timeoutspackage main
- Custom error handling
- Fork repository
- Create a feature branch
- Open a new pull request
- Create an issue for bug report or feature request
- Nguyen Duc Tam
- [email protected]
- http://twitter.com/tamnd87
The MIT License (MIT). Please see LICENSE for more information.
Copyright (c) 2015 Nguyen Duc Tam, [email protected]