Code Monkey home page Code Monkey logo

go-http's Introduction

gohttp

Build Status Go Report Card Coverage Status

基于 https://github.com/nahid/gohttp 改造

HTTP client for Go, its also support asynchronous request

Installation

go get github.com/nahid/gohttp

Example

POST https://httpbin.org/post

package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()

	resp, err := req.
		FormData(map[string]string{"name": "Nahid"}).
		Post("https://httpbin.org/post")

	if err != nil {
		panic(err)
	}

	if resp.GetStatusCode() == 200 {
		var resps map[string]interface{}

		_ = resp.GetBodyWithUnmarshal(&resps)
		fmt.Println(resps["form"])
	}
}

Async Example

package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()
	ch := make(chan *gohttp.AsyncResponse)

	var users [3]string

	users[0] = "nahid"
	users[1] = "shipu"
	users[2] = "sujan"

	for i:=0; i<len(users); i++ {
		req.
		FormData(map[string]string{"user": users[i]}).
		AsyncPost("http://domain.app/send", ch)
	}


	for i:=0; i<len(users); i++ {
		op := <-ch

		fmt.Println(op.GetBodyAsString())
	}
}

Available Method

  • NewRequest(options ...Option)

Request

  • Get(url string)
  • Post(url string)
  • Put(url string)
  • Patch(url string)
  • Delete(url string)

Async Request

  • AsyncGet(url string, ch chan)
  • AsyncPost(url string, ch chan)
  • AsyncPut(url string, ch chan)
  • AsyncPatch(url string, ch chan)
  • AsyncDelete(url string, ch chan)

Data Bindings

  • Headers(data map[string]string)
  • FormData(data map[string]string)
  • Json(data map[string]interface{})
  • Query(data map[string]string{})
  • Body(body []byte)
  • Text(text string)
  • BasicAuth(username, password string)
  • MultipartFormData(data map[string]string{})
  • Upload(name, file string)
  • Uploads(files map[string]string{})
  • UploadFromReader(param MultipartParam)
  • UploadsFromReader(params []MultipartParam)

Response

  • GetResp()
  • GetStatusCode()
  • GetBody()
  • GetBodyAsByte()
  • GetBodyAsString()
  • GetBodyWithUnmarshal(v interface{})

See API doc https://godoc.org/github.com/nahid/gohttp

go-http's People

Contributors

meiliangdeng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.