Categorygithub.com/Kaiser925/requests4go
repositorypackage
0.4.0
Repository: https://github.com/kaiser925/requests4go.git
Documentation: pkg.go.dev

# README

request4go

test Go Reference

Go HTTP Requests. ✨🎉✨ Send requests quickly and humanely.

Quick Start

Get module.

go get -u github.com/Kaiser925/requests4go

Make a request

package main

import (
	"log"

	"github.com/Kaiser925/requests4go"
)

func main() {
	r, err := requests4go.Get("http://httpbin.org/get")
	if err != nil {
		log.Fatal(err.Error())
	}

	txt, _ := r.Text()
	log.Println(txt)
}

You can also send a POST request.

package main

import (
	"log"

	"github.com/Kaiser925/requests4go"
)

func main() {
	// JSON will set body be json data.
	data := requests4go.JSON(requests4go.M{"key": "value"})
	r, err := requests4go.Post("http://httpbin.org/post", data)
	
	// handle r and err
}

Passing Parameters In URLS

params := requests4go.Params(requests4go.M{"key1": "value1", "key2": "value2"})
r, err := requests4go.Get("http://httpbin.org/get", params)

Custom Headers

	headers := requests4go.Headers(requests4go.M{"key1": "value1", "key2": "value2"})

	r, err := requests4go.Get("http://httpbin.org/get", headers)

Response Content

We can read the content of the server's response.

resp, _ := requests4go.Get("https://httpbin.org/get", nil)
txt, _ := resp.Text()
log.Println(txt)

JSON Response Content

There are two methods to handle JSON response content.

  1. We can deal with SimpleJSON witch use go-simplejson parse json data.
resp, _ := requests4go.Get("https://httpbin.org/get")
j, _ := resp.SimpleJSON()
url, _ := j.Get("url").String()
log.Println(url)
  1. We can unmarshal the struct by using JSON.
foo := &Foo{}
resp, _ := requests4go.Get("https://example.com")
j, _ := resp.JSON(foo)
log.Println(j.bar)

License

Apache License, Version 2.0. See LICENSE for the full license text