All Projects → kirinlabs → HttpRequest

kirinlabs / HttpRequest

Licence: Apache-2.0 License
Simplified HTTP client, A simplie golang HTTP client library.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to HttpRequest

request-extra
⚡️ Extremely stable HTTP request module built on top of libcurl with retries, timeouts and callback API
Stars: ✭ 14 (-89.55%)
Mutual labels:  curl, request
Request
A simple PHP HTTP request class.
Stars: ✭ 49 (-63.43%)
Mutual labels:  curl, request
Goz
A fantastic HTTP request libarary used in Golang.
Stars: ✭ 187 (+39.55%)
Mutual labels:  curl, request
go-axios
HTTP Request package for golang.
Stars: ✭ 29 (-78.36%)
Mutual labels:  curl, request
Urllib
Request HTTP(s) URLs in a complex world
Stars: ✭ 600 (+347.76%)
Mutual labels:  curl, request
Http Client
A high-performance, high-stability, cross-platform HTTP client.
Stars: ✭ 86 (-35.82%)
Mutual labels:  curl, request
Speedport-Plus-Cosmote-Router-hacks
Exploring the Sercomm made router of Cosmote - OTE Group (Deutsche Telekom in Greece)
Stars: ✭ 64 (-52.24%)
Mutual labels:  curl, httprequest
http
Aplus Framework HTTP Library
Stars: ✭ 113 (-15.67%)
Mutual labels:  request
AmxxCurl
Curl module for amxmodx
Stars: ✭ 28 (-79.1%)
Mutual labels:  curl
net
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.
Stars: ✭ 16 (-88.06%)
Mutual labels:  request
BusinessCard
💳 My curl-able business card
Stars: ✭ 49 (-63.43%)
Mutual labels:  curl
vue-methods-promise
Let Vue methods support return Promise
Stars: ✭ 35 (-73.88%)
Mutual labels:  request
email-checker
Provides email verification on the go.
Stars: ✭ 116 (-13.43%)
Mutual labels:  curl
ionic-push-php
ionic-push-php is a library that allows you to consume the Ionic Cloud API for sending push notifications (normal and scheduled), get a paginated list of sending push notifications, get information of registered devices, remove registered devices by token, ...
Stars: ✭ 20 (-85.07%)
Mutual labels:  curl
FritzBoxShell
Some shell scripts for controlling and checking the Fritz!Box/Fritz!Repeater
Stars: ✭ 80 (-40.3%)
Mutual labels:  curl
setup-linux-debian
Installs essential JavaScript development programs.
Stars: ✭ 16 (-88.06%)
Mutual labels:  curl
curly.hpp
Simple cURL C++17 wrapper
Stars: ✭ 48 (-64.18%)
Mutual labels:  curl
curl-fuzzer
Quality assurance testing for the curl project
Stars: ✭ 40 (-70.15%)
Mutual labels:  curl
servie
Standard, framework-agnostic HTTP interfaces for JavaScript servers and clients
Stars: ✭ 39 (-70.9%)
Mutual labels:  request
crosware
Tools, things, stuff, miscellaneous, etc., for Chrome OS / Chromium OS
Stars: ✭ 36 (-73.13%)
Mutual labels:  curl

HttpRequest

A simple HTTP Request package for golang. GET POST DELETE PUT

Installation

go get github.com/kirinlabs/HttpRequest

How do we use HttpRequest?

Create request object use http.DefaultTransport

resp, err := HttpRequest.Get("http://127.0.0.1:8000")
resp, err := HttpRequest.SetTimeout(5).Get("http://127.0.0.1:8000")
resp, err := HttpRequest.Debug(true).SetHeaders(map[string]string{}).Get("http://127.0.0.1:8000")

OR

req := HttpRequest.NewRequest()
req := HttpRequest.NewRequest().Debug(true).SetTimeout(5)
resp, err := req.Get("http://127.0.0.1:8000")
resp, err := req.Get("http://127.0.0.1:8000",nil)
resp, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest")
resp, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest","address=beijing")

Set headers

req.SetHeaders(map[string]string{
    "Content-Type": "application/x-www-form-urlencoded",
    "Connection": "keep-alive",
})

req.SetHeaders(map[string]string{
    "Source":"api",
})

Set cookies

req.SetCookies(map[string]string{
    "name":"json",
    "token":"",
})

OR

HttpRequest.SetCookies(map[string]string{
    "age":"19",
}).Post()

Set basic auth

req.SetBasicAuth("username","password")

Set timeout

req.SetTimeout(5)  //default 30s

Transport

If you want to customize the Client object and reuse TCP connections, you need to define a global http.RoundTripper or & http.Transport, because the reuse of the http connection pool is based on Transport.

var transport *http.Transport
func init() {   
    transport = &http.Transport{
        DialContext: (&net.Dialer{
            Timeout:   30 * time.Second,
            KeepAlive: 30 * time.Second,
            DualStack: true,
        }).DialContext,
        MaxIdleConns:          100, 
        IdleConnTimeout:       90 * time.Second,
        TLSHandshakeTimeout:   5 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }
}

func demo(){
    // Use http.DefaultTransport
    res, err := HttpRequest.Get("http://127.0.0.1:8080")
    // Use custom Transport
    res, err := HttpRequest.Transport(transport).Get("http://127.0.0.1:8080")
}

Keep Alives,Only effective for custom Transport

req.DisableKeepAlives(false)

HttpRequest.Transport(transport).DisableKeepAlives(false).Get("http://127.0.0.1:8080")

Ignore Https certificate validation,Only effective for custom Transport

req.SetTLSClient(&tls.Config{InsecureSkipVerify: true})

HttpRequest.Transport(transport).SetTLSClient(&tls.Config{InsecureSkipVerify: true}).Get("http://127.0.0.1:8080")

Object-oriented operation mode

req := HttpRequest.NewRequest().
	Debug(true).
	SetHeaders(map[string]string{
	    "Content-Type": "application/x-www-form-urlencoded",
	}).SetTimeout(5)
resp,err := req.Get("http://127.0.0.1")

resp,err := HttpRequest.NewRequest().Get("http://127.0.0.1")

GET

Query parameter

resp, err := req.Get("http://127.0.0.1:8000")
resp, err := req.Get("http://127.0.0.1:8000",nil)
resp, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest")
resp, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest","address=beijing")

OR

resp, err := HttpRequest.Get("http://127.0.0.1:8000")
resp, err := HttpRequest.Debug(true).SetHeaders(map[string]string{}).Get("http://127.0.0.1:8000")

Multi parameter

resp, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest",map[string]interface{}{
    "name":  "jason",
    "score": 100,
})
defer resp.Close()

body, err := resp.Body()
if err != nil {
    return
}

return string(body)

POST

// Send nil
resp, err := HttpRequest.Post("http://127.0.0.1:8000")

// Send integer
resp, err := HttpRequest.Post("http://127.0.0.1:8000", 100)

// Send []byte
resp, err := HttpRequest.Post("http://127.0.0.1:8000", []byte("bytes data"))

// Send io.Reader
resp, err := HttpRequest.Post("http://127.0.0.1:8000", bytes.NewReader(buf []byte))
resp, err := HttpRequest.Post("http://127.0.0.1:8000", strings.NewReader("string data"))
resp, err := HttpRequest.Post("http://127.0.0.1:8000", bytes.NewBuffer(buf []byte))

// Send string
resp, err := HttpRequest.Post("http://127.0.0.1:8000", "title=github&type=1")

// Send JSON
resp, err := HttpRequest.JSON().Post("http://127.0.0.1:8000", "{\"id\":10,\"title\":\"HttpRequest\"}")

// Send map[string]interface{}{}
resp, err := req.Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "HttpRequest",
})
defer resp.Close()

body, err := resp.Body()
if err != nil {
    return
}
return string(body)

resp, err := HttpRequest.Post("http://127.0.0.1:8000")
resp, err := HttpRequest.JSON().Post("http://127.0.0.1:8000",map[string]interface{}{"title":"github"})
resp, err := HttpRequest.Debug(true).SetHeaders(map[string]string{}).JSON().Post("http://127.0.0.1:8000","{\"title\":\"github\"}")

Jar

j, _ := cookiejar.New(nil)
j.SetCookies(&url.URL{
	Scheme: "http",
	Host:   "127.0.0.1:8000",
}, []*http.Cookie{
	&http.Cookie{Name: "identity-user", Value: "83df5154d0ed31d166f5c54ddc"},
	&http.Cookie{Name: "token_id", Value: "JSb99d0e7d809610186813583b4f802a37b99d"},
})
resp, err := HttpRequest.Jar(j).Get("http://127.0.0.1:8000/city/list")
defer resp.Close()

if err != nil {
	log.Fatalf("Request error:%v", err.Error())
}

Proxy

proxy, err := url.Parse("http://proxyip:proxyport")
if err != nil {
	log.Println(err)
}

resp, err := HttpRequest.Proxy(http.ProxyURL(proxy)).Get("http://127.0.0.1:8000/ip")
defer resp.Close()

if err != nil {
	log.Println("Request error:%v", err.Error())
}

body, err := resp.Body()
if err != nil {
	log.Println("Get body error:%v", err.Error())
}
log.Println(string(body))

Upload

Params: url, filename, fileinput

resp, err := req.Upload("http://127.0.0.1:8000/upload", "/root/demo.txt","uploadFile")
body, err := resp.Body()
defer resp.Close()
if err != nil {
    return
}
return string(body)

Debug

Default false

req.Debug(true)

Print in standard output:

[HttpRequest]
-------------------------------------------------------------------
Request: GET http://127.0.0.1:8000?name=iceview&age=19&score=100
Headers: map[Content-Type:application/x-www-form-urlencoded]
Cookies: map[]
Timeout: 30s
ReqBody: map[age:19 score:100]
-------------------------------------------------------------------

Json

Post JSON request

Set header

 req.SetHeaders(map[string]string{"Content-Type": "application/json"})

Or

req.JSON().Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "github",
})

req.JSON().Post("http://127.0.0.1:8000", "{\"title\":\"github\",\"id\":10}")

Post request

resp, err := req.Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "HttpRequest",
})

Print formatted JSON

str, err := resp.Export()
if err != nil {
   return
}

Unmarshal JSON

var u User
err := resp.Json(&u)
if err != nil {
   return err
}

var m map[string]interface{}
err := resp.Json(&m)
if err != nil {
   return err
}

Response

Response() *http.Response

resp, err := req.Post("http://127.0.0.1:8000/") //res is a http.Response object

StatusCode() int

resp.StatusCode()

Body() ([]byte, error)

body, err := resp.Body()
log.Println(string(body))

Close() error

resp.Close()

Time() string

resp.Time()  //ms

Print formatted JSON

str, err := resp.Export()
if err != nil {
   return
}

Unmarshal JSON

var u User
err := resp.Json(&u)
if err != nil {
   return err
}

var m map[string]interface{}
err := resp.Json(&m)
if err != nil {
   return err
}

Url() string

resp.Url()  //return the requested url

Headers() http.Header

resp.Headers()  //return the response headers
resp.Headers().Get("Content-Type")

Cookies() []*http.Cookie

resp.Cookies()  //return the response cookies

Advanced

GET

import "github.com/kirinlabs/HttpRequest"
   
resp,err := HttpRequest.Get("http://127.0.0.1:8000/")
resp,err := HttpRequest.Get("http://127.0.0.1:8000/","title=github")
resp,err := HttpRequest.Get("http://127.0.0.1:8000/?title=github")
resp,err := HttpRequest.Debug(true).JSON().Get("http://127.0.0.1:8000/")

POST

import "github.com/kirinlabs/HttpRequest"
   
resp,err := HttpRequest.Post("http://127.0.0.1:8000/")
resp,err := HttpRequest.SetHeaders(map[string]string{
	"title":"github",
}).Post("http://127.0.0.1:8000/")
resp,err := HttpRequest.Debug(true).JSON().Post("http://127.0.0.1:8000/")

Example

import "github.com/kirinlabs/HttpRequest"
   
resp,err := HttpRequest.Get("http://127.0.0.1:8000/")
resp,err := HttpRequest.Get("http://127.0.0.1:8000/","title=github")
resp,err := HttpRequest.Get("http://127.0.0.1:8000/?title=github")
resp,err := HttpRequest.Get("http://127.0.0.1:8000/",map[string]interface{}{
	"title":"github",
})
resp,err := HttpRequest.Debug(true).JSON().SetHeaders(map[string]string{
	"source":"api",
}).SetCookies(map[string]string{
	"name":"httprequest",
}).Post("http://127.0.0.1:8000/")


//Or
req := HttpRequest.NewRequest()
req := req.Debug(true).SetHeaders()
resp,err := req.Debug(true).JSON().SetHeaders(map[string]string{
    "source":"api",
}).SetCookies(map[string]string{
    "name":"httprequest",
}).Post("http://127.0.0.1:8000/")
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].