httpclient

package module
v0.0.0-...-b92a0ec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2015 License: MIT Imports: 7 Imported by: 21

README

HttpClient

Software License Build Status GoDoc

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"}
Why I made it?

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.

When to use it?
  • 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.
Is it any good?

May be.

Features

  • Get and unmarshal JSON from a url
  • Get and unmarshal XML from a url
  • Download multipe files concurrency

Install

$ go get github.com/tamnd/httpclient

Usage

import "github.com/tamnd/httpclient"

How to

Get String
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")
Get Bytes
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")
Get JSON
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)
Download Files
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)
Get Reader from Response
func Reader(url string) (io.ReadCloser, error)

Reader issues a GET request to a specified URL and returns an reader from the response body.

Roadmap

  • Send POST request
  • Custom request header
  • Send basic authentication
  • Make Upload() function
  • Get response header
  • Connection timeoutspackage main
  • Custom error handling

Contribute

  • Fork repository
  • Create a feature branch
  • Open a new pull request
  • Create an issue for bug report or feature request

Contact

License

The MIT License (MIT). Please see LICENSE for more information.

Copyright (c) 2015 Nguyen Duc Tam, tamnd87@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(url string) ([]byte, error)

Bytes fetches the specified url and returns the response body as bytes.

func Download

func Download(urls []string, files *[]File) error

Download downloads multiple files concurrency.

func Files

func Files(urls []string, files *[]File) error

Files downloads multiple files concurrency.

func Get

func Get(url string) (*http.Response, error)

Get issues a GET to the specified URL. It returns an http.Response for further processing.

func JSON

func JSON(url string, v interface{}) error

JSON issues a GET request to a specified URL and unmarshal json data from the response body.

func New

func New() *httpClient

New returns new client.

func Reader

func Reader(url string) (io.ReadCloser, error)

Reader issues a GET request to a specified URL and returns an reader from the response body.

func String

func String(url string) (string, error)

String fetches the specified URL and returns the response body as a string.

func XML

func XML(url string, v interface{}) error

XML issues a GET request to a specified URL and unmarshal xml data from the response body.

Types

type Error

type Error struct {
	Message    string
	StatusCode int
	URL        string
}

Error is the custom error type returns from HTTP requests.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

type File

type File struct {
	// File name with no directory.
	Name string

	// Contents of the file.
	Data []byte
}

File represents a file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL