Giter Club home page Giter Club logo

requests's Introduction

requests for Golang

一个 Golang 版本的 requests 网络请求库(类似于 python 版本),方便苦于 Golang 繁琐的 http 请求的 Gopher 使用

构思

  • 关键字参数
    Golang 没有关键字参数,通过自定义类型 + 参数类型断言模拟。 如地址栏参数在 python 版本中使用 params = {"abc":456} 传入, 在 Golang 版本中通过 Params{"abc":456} 传入。

使用

  • 请求体使用 可以传入 Headers、Params、Cookies、Xform、FormData、FileBody、JsonBody、StringBody、BytesBody 等多种参数

  • 响应体使用
    为响应体添加了 String、Dict、List、Object、Json、Discard、Bytes、AutoObject、File 等方法,用于不同的解析方式。

    • File 方法用于保存文件或流式接收,不会缓存响应内容(不限响应体大小)。
    • 其他方法在解析同时会保存响应内容,可以多次使用不同解析方式处理(建议用于响应体不大的情况)。
    • AutoObject 方法可以根据 Content-Type 自动选择解析 JSON/XML/FormData 格式
  • Find 方法
    使用多级参数查找一个对象中的元素,可用于读取配置文件或网络返回的 JSON 对象

    • 函数定义: func Find(obj interface{}, args ...interface{}) (interface{}, bool)
      • args 中的元素可以是 string 或 int, string 代表查询的是字典, int 代表查询的是数组
      • 如 JSON 对象 {"abc":[123,456,789]} 中的 789 可以使用参数 ["abc",2] 查出
      • 再如 JSON 对象 ["abc",[123,456,789,{"def":111}]] 中的 111 可以使用参数 [1,3,"def"] 查出
      • 再如 JSON 对象 ["abc",[123,456,789,{"def":111}]] 中的 {"def":111} 可以使用参数 [1,3] 查出
      • 返回值分别为查找结果和是否找到

    包装了常用的 int int64 uint string float32 之类的方法

  • 中间件

    • 请求中间件
      通过 AddReqCB 方法添加,在生成 SRequest 结构体之后执行。可用于认证之类的操作
    • 响应中间件 通过 AddRespCB 方法添加,在生成 SResponse 结构体之后执行。可用于验证响应之类的操作
  • 使用例子
    获取 https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=7 并解析

package main

import (
	"fmt"

	"github.com/wond4/requests"
)

func main() {
	fmt.Println("start")
	resp, _ := requests.Get("https://cn.bing.com/HPImageArchive.aspx",
		requests.Params{"format": "js", "idx": "0", "n": "7"})
	if resp.Resp.StatusCode != 200 {
		fmt.Printf("response code err. expect %v, get %v", 200, resp.Resp.StatusCode)
	}
	res, _ := resp.Object()
	url, ok := requests.FindString(res, "images", 1, "url")
	if !ok {
		fmt.Println("data parse err.")
	}
	fmt.Println(url)
}

requests's People

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.