Giter Site home page Giter Site logo

go-curl's Introduction

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

go-curl's People

Contributors

likeaixi avatar

Watchers

James Cloos avatar  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.