Giter VIP home page Giter VIP logo

request's Introduction

Go Report Card License GoDoc Build Status Coverage Status

request

A wrapper for net/http client

Example Customize url query with struct tag rq

func TestRequestQuery(t *testing.T) {
	client := &http.Client{}
	ctx := context.Background()
	rp, err := NewRequestProvider(client)
	require.NoError(t, err)
	r, err := NewRequestWithContext(
		ctx,
		http.MethodGet,
		"https://jsonplaceholder.typicode.com/posts",
		struct {
			UserId string `rq:"userId"`
		}{
			UserId: "1",
		},
		nil,
	)
	require.NoError(t, err)
	require.Equal(t, "https://jsonplaceholder.typicode.com/posts?userId=1", r.URL.String())
	res, err := rp.Do(r)
	require.NoError(t, err)
	res.Body.Close()

}

Example Customize header with struct tag rh

func TestRequestHeader(t *testing.T) {
	client := &http.Client{}
	ctx := context.Background()
	rp, err := NewRequestProvider(client)
	require.NoError(t, err)

	bs, err := json.Marshal(&struct {
		Title  string `json:"title"`
		Body   string `json:"bar"`
		UserId int    `json:"userId"`
	}{"foo", "bar", 1})
	require.NoError(t, err)

	r, err := NewRequestWithContext(
		ctx,
		http.MethodPost,
		"https://jsonplaceholder.typicode.com/posts",
		struct {
			ContentType string `rh:"Content-Type"`
		}{
			ContentType: "application/json; charset=UTF-8",
		},
		bytes.NewReader(bs),
	)
	require.NoError(t, err)
	require.Equal(t, "application/json; charset=UTF-8", r.Header.Get("Content-Type"))
	res, err := rp.Do(r)
	require.NoError(t, err)
	res.Body.Close()
}

request's People

Contributors

kyicy 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.