如何将此curl调用转换为Go代码?

huangapple go评论67阅读模式
英文:

How to translate this curl call into Go?

问题

我可以帮你将curl调用翻译成Go代码。以下是翻译好的代码:

import (
	"net/http"
	"net/url"
)

func makeRequest() error {
	u, err := url.Parse("https://www.myexample.com" + "/mypath")
	if err != nil {
		return err
	}
	q := u.Query()
	q.Set("param1", "foobar")
	u.RawQuery = q.Encode()

	req, err := http.NewRequest(http.MethodPost, u.String(), nil)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Authorization", "Basic XXXX")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// 处理响应

	return nil
}

在这段代码中,我添加了设置请求头的代码和设置数据字段的代码。你可以根据需要修改数据字段的值。请注意,你需要将XXXX替换为正确的授权信息。

英文:

I have the following curl call:

curl \
--request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--header "Authorization: Basic XXXX" \
--data-urlencode 'A=B' \
--data-urlencode 'C=D' \
"https://www.myexample.com/mypath?param1=foobar"

How can I write Go code that will do the exact same thing?

So far, I have successfully constructed the full URL and am sending it to http.NewRequestWithContext()

u, err := url.Parse("https://www.myexample.com" + "/mypath")
if err != nil {
	return err
}
q := u.Query()
u.RawQuery = q.Encode()


http.NewRequestWithContext(myContext, http.MethodPost, u.String(), nil)
response, err := http.NewRequestWithContext(myContext, http.MethodPost, s.cfg.BaseDRAPI + oAuthPath, nil)
if err != nil {
	return err
}

However, I do not know where/how to specify the headers and the two data fields.
Please help!

答案1

得分: 2

创建请求并按照以下方式发送。根据你的应用程序处理错误和响应。

    endpoint := "https://www.myexample.com/mypath?param1=foobar"
	data := url.Values{}
	data.Set("A", "B")
	data.Set("C", "D")

	// 创建一个新的POST请求,并将表单数据进行编码
	r, err := http.NewRequest("POST", endpoint, strings.NewReader(data.Encode())) // URL-encoded payload
	if err != nil {
		log.Fatal(err)
	}

	// 设置请求头
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")  // 这是表单数据编码请求的必需项
	r.Header.Add("Authorization", "Basic XXXX")

	// 发送请求并获取响应
	client := &http.Client{}
	res, err := client.Do(r)
	if err != nil {
		// 处理错误
		log.Fatal("error: ", err)
	}
	log.Println(res.Status)
	// 在这里处理响应部分
英文:

Create the request and send it as below. Handle the error and response according to your application.

    endpoint := "https://www.myexample.com/mypath?param1=foobar"
	data := url.Values{}
	data.Set("A", "B")
	data.Set("C", "D")

	//create new POST request to the url and encoded form Data
	r, err := http.NewRequest("POST", endpoint, strings.NewReader(data.Encode())) // URL-encoded payload
	if err != nil {
		log.Fatal(err)
	}

	//set headers to the request
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")  //this is a must for form data encoded request
	r.Header.Add("Authorization", "Basic XXXX")

	//send request and get the response
	client := &http.Client{}
	res, err := client.Do(r)
	if err != nil {
		//handle error
		log.Fatal(`error: `,err)
	}
	log.Println(res.Status)
	//handle response part here

huangapple
  • 本文由 发表于 2021年8月7日 00:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/68684593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定