GoCurlTool

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 0

README

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代理设置

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpGet

func HttpGet(url string, queries map[string]string) (string, error)

* * get请求 * @param url string 请求地址 * @param queries map[string]string 请求参数 * @return string,error

func HttpPost

func HttpPost(url string, queries map[string]string, postData map[string]interface{}) (string, error)

* * get请求 * @param url string 请求地址 * @param queries map[string]string 请求参数 * @return string,error

Types

type Request

type Request struct {
	Raw      *http.Request
	Method   string
	Url      string
	Headers  map[string]string
	Cookies  map[string]string
	Queries  map[string]string
	PostData map[string]interface{}
	// contains filtered or unexported fields
}

Request构造类

func NewRequest

func NewRequest() *Request

创建一个Request实例

func (*Request) Get

func (this *Request) Get() (*Response, error)

发起get请求

func (*Request) Post

func (this *Request) Post() (*Response, error)

发起post请求

func (*Request) Send

func (this *Request) Send(url string, method string) (*Response, error)

发起请求

func (*Request) SetCookies

func (this *Request) SetCookies(cookies map[string]string) *Request

设置请求cookies

func (*Request) SetHeaders

func (this *Request) SetHeaders(headers map[string]string) *Request

设置请求头

func (*Request) SetMethod

func (this *Request) SetMethod(method string) *Request

设置请求方法

func (*Request) SetPostData

func (this *Request) SetPostData(postData map[string]interface{}) *Request

设置post请求的提交数据

func (*Request) SetQueries

func (this *Request) SetQueries(queries map[string]string) *Request

设置url查询参数

func (*Request) SetUrl

func (this *Request) SetUrl(url string) *Request

设置请求地址

type Response

type Response struct {
	Raw     *http.Response
	Headers map[string]string
	Body    string
}

func NewResponse

func NewResponse() *Response

func (*Response) IsOk

func (this *Response) IsOk() bool

Jump to

Keyboard shortcuts

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