Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/ido50/requests

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ido50/requests

  • v1.6.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

requests

high-level HTTP client for Go.


requests is a high-level, API-centric HTTP client for Go projects. It is meant to provide a more comfortable mechanism to perform requests to HTTP APIs (rather than making general requests), and to prevent common mistakes made when using net/http directly.

With requests, one must not need to remember to read HTTP responses in full (so Go can reuse TCP connections), nor to close response bodies. Handling of JSON data - be it in requests or responses - is made easier by way of built-in encoders/decoders. An automatic retry mechanism is also included.

The library allows a "DRY" (Dont Repeat Yourself) approach to REST API usage by introducing API-specific dependencies into the client object. For example, authorization headers and response handlers can be set in the client object, and all generated requests will automatically include them.

Install

go get -u github.com/ido50/requests

Usage

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/ido50/requests"
)

const apiURL = "https://my.api.com/v2"

type RequestBody struct {
	Title   string   `json:"title"`
	Tags    []string `json:"tags"`
	Publish bool     `json:"publish"`
}

type ResponseBody struct {
	ID   int64     `json:"id"`
	Date time.Time `json:"date"`
}

func main() {
	client := requests.
		NewClient(apiURL).
		Accept("application/json").
		BasicAuth("user", "pass").
		RetryLimit(3)

	var res ResponseBody

	err := client.
		NewRequest("POST", "/articles").
		JSONBody(RequestBody{
			Title:   "Test Title",
			Tags:    []string{"test", "stories"},
			Publish: true,
		}).
		ExpectedStatus(http.StatusCreated).
		Into(&res).
		Run()
	if err != nil {
		panic(err)
	}

	fmt.Printf("Created article %d on %s\n", res.ID, res.Date.Format(time.RFC3339))
}

FAQs

Package last updated on 31 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc