httpx

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 10 Imported by: 0

README

httpx

A simple, chainable HTTP client for Go with session support.

Installation

go get github.com/Grizz1ya/httpx

Features

  • Session management with cookie persistence
  • Chainable request building
  • Support for different request formats (JSON, form data, raw body)
  • Proxy support
  • Static and request-specific headers

Usage

Basic Requests

Make a simple GET request:

import "github.com/Grizz1ya/httpx"

// Simple GET request
response, err := httpx.Get("https://example.com").Do()
if err != nil {
    // handle error
}
fmt.Println(response.Text())

// GET with query parameters
response, err = httpx.Get("https://example.com").Params(map[string]interface{}{
    "page": "1",
    "limit": "10",
}).Do()

// POST with JSON body
response, err = httpx.Post("https://example.com/api").Json(map[string]interface{}{
    "username": "user",
    "password": "pass",
}).Do()
Session Management

Create a session to reuse cookies and headers across requests:

// Create a new session
session := httpx.NewSession()

// Add static headers that will be used in all requests
session.AddStaticHeader("User-Agent", "My Custom User Agent")

// Add cookies
session.AddCookie("example.com", "session_id", "abc123")

// Make a request using the session
response, err := session.Get("https://example.com").Do()

// Remove headers or cookies when needed
session.RemoveStaticHeader("User-Agent")
session.RemoveCookie("example.com", "session_id")
Using Proxies
// Create a proxy from URL string
proxy, err := httpx.NewProxyFromLine("http://127.0.0.1:8080")
if err != nil {
    // handle error
}

// Set proxy to a session
session := httpx.NewSession()
err = session.SetProxy(proxy)
if err != nil {
    // handle error
}

// Remove proxy
session.SetProxy(nil)
Request Customization
// Add headers to a specific request
response, err := session.Get("https://example.com").Headers(map[string]string{
    "X-Custom-Header": "value",
}).Do()

// Add form data to a POST request
response, err = session.Post("https://example.com/form").Data(map[string]interface{}{
    "field1": "value1",
    "field2": "value2",
}).Do()

// Add JSON to a POST request
response, err = session.Post("https://example.com/api").Json(map[string]interface{}{
    "key": "value",
}).Do()

// Set a raw body
response, err = session.Post("https://example.com/api").Body([]byte("raw body content")).Do()
Response Handling
// Get response as text
response, _ := httpx.Get("https://example.com").Do()
textContent := response.Text()

// Parse JSON response
jsonResponse := response.Json()
value := jsonResponse.Get("key").String()

// Access cookies
cookies := response.Cookies()

Example

package main

import (
    "fmt"
    "github.com/Grizz1ya/httpx"
)

func main() {
    // Create a session
    session := httpx.NewSession()
    session.AddStaticHeader("User-Agent", "Mozilla/5.0")

    // Set up a proxy
    proxy, err := httpx.NewProxyFromLine("http://127.0.0.1:8080")
    if err == nil {
        session.SetProxy(proxy)
    }

    // Make a request
    response, err := session.Get("https://api.example.com/data").Params(map[string]interface{}{
        "limit": "10",
    }).Do()

    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    // Parse JSON response
    json := response.Json()
    items := json.GetArray("items")

    for _, item := range items {
        fmt.Println(item.Get("name").String())
    }
}

License

MIT License

Copyright (c) [2025] [Grizz1ya]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Proxy

type Proxy struct {
	Scheme   string // e.g., "http", "https", "socks4", "socks5"
	Host     string
	Port     string
	Username *string // optional
	Password *string // optional
}

func NewProxy

func NewProxy(opt *Proxy) (*Proxy, error)

func NewProxyFromLine

func NewProxyFromLine(rawProxy string) (*Proxy, error)

func (*Proxy) IsAuth

func (p *Proxy) IsAuth() bool

func (*Proxy) String

func (p *Proxy) String() string

func (*Proxy) TransportFunction

func (p *Proxy) TransportFunction() (func(*http.Request) (*url.URL, error), error)

type Request

type Request struct {
	// contains filtered or unexported fields
}

func Get

func Get(url string) *Request

* Static methods

func Post

func Post(url string) *Request

func (*Request) Body

func (r *Request) Body(body []byte) *Request

func (*Request) Data

func (r *Request) Data(data map[string]interface{}) *Request

func (*Request) Do

func (r *Request) Do() (*Response, error)

func (*Request) Headers

func (r *Request) Headers(headers map[string]string) *Request

func (*Request) Json

func (r *Request) Json(_json map[string]interface{}) *Request

func (*Request) Params

func (r *Request) Params(params map[string]interface{}) *Request

type Response

type Response struct {
	// contains filtered or unexported fields
}

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

func (*Response) Json

func (r *Response) Json() *gofasion.Fasion

func (*Response) Text

func (r *Response) Text() string

type Session

type Session struct {
	Proxy *Proxy
	// contains filtered or unexported fields
}

func NewSession

func NewSession() *Session

func (*Session) AddCookie

func (s *Session) AddCookie(domain, name, value string)

func (*Session) AddStaticHeader

func (s *Session) AddStaticHeader(key, value string)

func (*Session) Get

func (s *Session) Get(url string) *Request

func (*Session) Options

func (s *Session) Options(url string) *Request

func (*Session) Post

func (s *Session) Post(url string) *Request

func (*Session) Put added in v0.1.8

func (s *Session) Put(url string) *Request

func (*Session) Redirect added in v0.2.0

func (s *Session) Redirect(enable bool)

func (*Session) RemoveCookie

func (s *Session) RemoveCookie(domain, name string)

func (*Session) RemoveStaticHeader

func (s *Session) RemoveStaticHeader(key string)

func (*Session) SetProxy

func (s *Session) SetProxy(proxy *Proxy) error

Jump to

Keyboard shortcuts

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