requests

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 4 Imported by: 0

README

Go Requests

Language Last Commit CI codecov Benchmark Go Reference Go Report Card

English | 简体中文

Go Requests is a simple and easy-to-use HTTP request library for Go, inspired by Python's Requests library. It provides an easy-to-understand and use API designed to simplify HTTP requests in Go.

Features

  • Easy-to-use HTTP request API
  • Supports common HTTP request methods such as GET, POST, PUT, DELETE, etc.
  • Automatically handles headers, URL encoding, JSON encoding/decoding, etc.
  • Supports HTTP request timeout configuration
  • Supports file upload and download
  • Supports proxy settings
  • Supports DNS server settings
  • Supports request redirection and cookie management

Install

Install via Go modules:

go get github.com/sunerpy/requests

Example

Making a GET Request
package main

import (
    "fmt"
    "log"

    "github.com/sunerpy/requests"
)

func main() {
    resp, err := requests.Get("https://api.github.com",nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Status Code:", resp.StatusCode)
    fmt.Println("Response Body:", resp.Text())
}
Making a POST Request
package main

import (
    "fmt"
    "log"

    "github.com/sunerpy/requests"
)

func main() {
    resp, err := requests.Post("https://httpbin.org/post", nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Status Code:", resp.StatusCode)
    fmt.Println("Response JSON:", resp.JSON())
}
File Upload
package main

import (
    "fmt"
    "log"

    "github.com/sunerpy/requests"
)

func main() {
    resp, err := requests.Post("https://httpbin.org/post", nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Response JSON:", resp.JSON())
}
Setting Request Timeout
package main

import (
    "fmt"
    "log"
    "time"

    "github.com/sunerpy/requests"
)

func main() {
    session := requests.NewSession()
    session.SetTimeout(5 * time.Second)
	req, err := requests.NewRequest("GET", "https://httpbin.org/get", nil, nil)
	if err != nil {
        log.Fatal(err)
    }
    resp, err := session.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Response:", resp.Text())
}

API

requests.Get(baseURL string, params *url.Values) (*models.Response, error)

Initiates a GET request。

  • url:The URL of the request。
requests.Post(baseURL string, params *url.Values) (*models.Response, error)

Initiates a POST request。

  • url:The URL of the request。
requests.NewSession() Session

Creates a new session, allowing you to set default configurations for requests, such as timeouts, proxies, etc.

(*Session) SetTimeout(timeout time.Duration)

Sets the timeout for the request.

(*Response) JSON() interface{}

Parses the response JSON data and returns it.

(*Response) Text() string

Returns the text content of the response.

Contributions

Contributions are welcome! If you're interested in contributing, please follow these steps:

  1. Fork this repository.
  2. Make changes in your fork.
  3. Submit a pull request.

License

Go Requests is an open-source project licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(baseURL string, params *url.Values) (*models.Response, error)

Delete 发送 DELETE 请求

func Get

func Get(baseURL string, params *url.Values) (*models.Response, error)

func NewRequest

func NewRequest(method, rawURL string, params *url.Values, body io.Reader) (*requests.Request, error)

func NewSession

func NewSession() requests.Session

func Patch

func Patch(baseURL string, form *url.Values) (*models.Response, error)

Patch 发送 PATCH 请求

func Post

func Post(baseURL string, form *url.Values) (*models.Response, error)

Post 发送 POST 请求

func Put

func Put(baseURL string, form *url.Values) (*models.Response, error)

Put 发送 PUT 请求

func SetHTTP2Enabled

func SetHTTP2Enabled(enabled bool)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL