登录
首页 >  Golang >  Go问答

将curl请求转换为Golang

来源:stackoverflow

时间:2024-04-13 11:45:32 171浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《将curl请求转换为Golang》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

因此,对 paypal payouts api 的这个curl请求有效:

curl --silent -v 'https://api.sandbox.paypal.com/v1/oauth2/token' \
   -h "accept: application/json" \
   -h "accept-language: en_us" \
   -u "${client_id}:${client_secret}" \
   -d "grant_type=client_credentials"

有一件事我很困惑:-d 选项用于 http 请求正文中的数据 - -d 选项是否使其成为 post 请求,或者是带有正文的 get 请求之上的curl 请求?我猜是后者,但考虑到 curl --help 的输出,我不确定。

在 golang 中我有:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

const (
    PayPalTestClientID = "Aeit5RskDRN8eUUMB0Ud3RjA_z6feWMUHktwlJZMeQMo9A9ulbKK"
    PayPalTestSecret   = "EAAqyzrOTUWf-OFJCB4BxgXT4xuravL7pnkC8Tn20HYtZExd1mFO"
)

func main() {

    //reader := bytes.NewBuffer()
    req, err := http.NewRequest("GET", "https://api.sandbox.paypal.com/v1/oauth2/token", nil)

    if err != nil {
        log.Fatal(err)
    }

    req.Header.Set("Accept", "application/json")
    req.Header.Set("Accept-Language", "en_US")
    req.Header.Set("Authorization", fmt.Sprintf("Basic %s:%s", PayPalTestClientID, PayPalTestSecret))

    client := &http.Client{}
    resp, err := client.Do(req)

    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        log.Fatal(err)
    }

    var v interface{}
    err = json.Unmarshal(body, &v);

    if err != nil {
        log.Fatal(err)
    }

    log.Print(v)
}

客户端/秘密被混淆了,所以它们不会像上面写的那样工作。但使用我得到的真实信用:

2020/01/31 16:05:07 地图[错误:invalid_client error_description:客户端 认证失败]

真正的信用确实可以与curl命令一起使用。


解决方案


注意:提供的凭据有效吗?因为我收到了 sonorus 401,authentication failed

注意:在 curl 中使用 -d,您将发送 post 请求,而不是 get。由于这种行为,您可能想要发送 post 请求而不是 get

你可以使用我的小http库:https://github.com/alessiosavi/Requests/

package main

import requests "github.com/alessiosavi/requests"

func main() {
    req, err := requests.initrequest("https://postman-echo.com/basic-auth", "get", []byte{}, false, false)
    if err != nil {
        fmt.println("error! ", err)
    }
    req.createheaderlist("accept", "application/json", "accept-language", "en_us", "authorization", "postman:password")
    client := &http.client{}
    resp := req.executerequest(client)
    fmt.println(resp.dump())
}

您可以使用服务身份验证所需的数据(url、发布数据、标头)更改数据。

就您而言,将是这样的:

package main

import requests "github.com/alessiosavi/Requests"

const (
    ID= "Aeit5RskDRN8eUUMB0Ud3RjA_z6feWMUHktwlJZMeQMo9A9ulbKK"
    SECRET= "EAAqyzrOTUWf-OFJCB4BxgXT4xuravL7pnkC8Tn20HYtZExd1mFO"
)

func main() {
    req, err := requests.InitRequest("https://api.sandbox.paypal.com/v1/oauth2/token", "GET", []byte{"grant_type=client_credentials"}, false, false)
    if err != nil {
        fmt.Println("ERROR! ", err)
    }
    req.CreateHeaderList("Accept", "application/json", "Accept-Language", "en_US", "Authorization", ID+":"+SECRET)
    client := &http.Client{}
    resp := req.ExecuteRequest(client)
    fmt.Println(resp.Dump())
}

以上就是《将curl请求转换为Golang》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>