首页 > go > GO curl获取远程数据

GO curl获取远程数据

GO这种强类型语言,写起来真是头疼

 

  package main

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

func gocurl() string {

	url := "http://127.0.0.1/bbb.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{}{
		"id": "230012",
		"m":  2,
	}

	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.Print(resp.Body)
		return resp.Body
	} else {

		return "err"
		//fmt.Println(resp.Raw)
	}

}

func main() {

	msgs := gocurl()
	fmt.Print(msgs)
	
}