Skip to content
golang版本的curl请求库
Go
Branch: master
Clone or download
Latest commit fabfc3d Sep 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Sep 10, 2017
README.md
go-curl.png 0.1.0正式版 Sep 14, 2017
request.go add more requests Sep 28, 2018
response.go 0.1.0正式版 Sep 14, 2017

README.md

golang版本的curl请求库

安装

go get github.com/mikemintang/go-curl

使用

package main

import (
    "fmt"
    "github.com/mikemintang/go-curl"
)

func main() {

    url := "http://php.dev/api.php"

    headers := map[string]string{
        "User-Agent":    "Sublime",
        "Authorization": "Bearer access_token",
        "Content-Type":  "application/json",
    }

    cookies := map[string]string{
        "userId":    "12",
        "loginTime": "15045682199",
    }

    queries := map[string]string{
        "page": "2",
        "act":  "update",
    }

    postData := map[string]interface{}{
        "name":      "mike",
        "age":       24,
        "interests": []string{"basketball", "reading", "coding"},
        "isAdmin":   true,
    }

    // 链式操作
    req := curl.NewRequest()
    resp, err := req.
        SetUrl(url).
        SetHeaders(headers).
        SetCookies(cookies).
        SetQueries(queries).
        SetPostData(postData).
        Post()

    if err != nil {
        fmt.Println(err)
    } else {
        if resp.IsOk() {
            fmt.Println(resp.Body)
        } else {
            fmt.Println(resp.Raw)
        }
    }

}

接收请求的api.php

<?php  

//echo json_encode($_GET);                      // 获取url地址中的查询参数
//echo json_encode(getallheaders());            // 获取请求头
//echo json_encode($_COOKIE);                   // 获取cookies
echo file_get_contents("php://input");          // 获取post提交的数据

function getallheaders() { 
    $headers = []; 
    foreach ($_SERVER as $name => $value) { 
       if (substr($name, 0, 5) == 'HTTP_') { 
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
       } 
    } 
    return $headers; 
} 

可导出的成员变量和方法

TodoList

  • 以链式操作的方式发起请求
  • 以函数回调的方式发起请求
  • 以类Jquery Ajax的方式发起请求
  • 发起GET/POST请求
  • 发起PUT/PATCH/DELETE/OPTIONS操作
  • 以application/x-www-form-urlencoded形式提交post数据
  • 以application/json形式提交post数据
  • 以multipart/form-data形式提交post数据
  • proxy代理设置
You can’t perform that action at this time.