golang 中的 HTTPS POST 请求不如 Python 中预期的那样工作
时间:2024-03-23
浏览:334
作者:佚名
在 Python 中,可以使用 `requests` 库轻松实现 HTTPS POST 请求。然而,在 Go 中,需要明确发送 JSON 数据。示例代码展示了如何将 JSON 数据发送到端点,而使用查询参数则需要使用 GET 方法。为了实现并发,可以使用 `sync.WaitGroup` 和 `go` 关键字来创建协程。
问题内容
我正在尝试从 jira rest api 示例实现 python 代码:
https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-jql/#api-rest-api-3-jql-parse-post
我的python代码(按预期工作):
import requests
from requests.auth import httpbasicauth
import json
url = "https://my-url.com/rest/api/2/search"
auth = httpbasicauth("user1", "pwd1")
headers = {
"accept": "application/json",
"content-type": "application/json"
}
payload = json.dumps( {
"jql": "my-query-string"
}
response = requests.request("post", url, data=payload, headers=headers, auth=auth, verify=false)
print(json.dumps(json.loads(response.text), sort_keys=true, indent=4, separators=(",", ": ")))
我正在尝试将其转换为 golang 代码,如下所示:
package main
import (
"io/ioutil"
"fmt"
"log"
"time"
"net/http"
"net/url"
}
func main() {
timeout := time.duration(500 * time.second)
client := http.client{
timeout: timeout,
}
req, err := http.newrequest("post", "https://my-url.com/rest/api/2/search", nil)
if err != nil {
log.fatalln(err)
}
req.setbasicauth("user1", "pwd1")
req.header.set("content-type", "application/json")
q := url.values{}
q.add("jql", "my-query-string")
req.url.rawquery = q.encode()
fmt.println(req.url.string())
resp, err := client.do(req)
if err != nil {
log.fatalln(err)
}
defer resp.body.close()
data, err := ioutil.readall(resp.body)
if err != nil {
log.fatalln(err)
}
log.println(string(data))
代码构建没有问题。当我运行 go 代码时,出现此错误:
2021/04/17 19:36:31 {"errormessages":["no content to map to object due to end of input"]}
我有 2 个问题:
a. How can I fix the above error ? b. I also want to include concurrency in the same code, i.e the same POST request will actually be executed for 5 different query strings (concurrently) and fetch the results, how can i achieve that ?
正确答案
对于 POST 请求,您需要以 json 形式发送数据。请注意,在 go 中设置请求的 content-type 标头不会自动将您提供的任何内容转换为指定类型。
发送 json 的示例。
package main
import (
"strings"
"net/http"
"io/ioutil"
"fmt"
)
func main() {
body := strings.newreader(`{"jql": "project = hsp"}`)
req, err := http.newrequest("post", "https://your-domain.atlassian.com/rest/api/2/search", body)
if err != nil {
panic(err)
}
req.setbasicauth("[email protected]", "<api_token>")
req.header.set("accept", "application/json")
req.header.set("content-type", "application/json")
resp, err := http.defaultclient.do(req)
if err != nil {
panic(err)
}
defer resp.body.close()
out, err := ioutil.readall(resp.body)
if err != nil {
panic(err)
}
fmt.println(string(out))
}
如果您想使用查询参数,您应该使用带有 GET 方法的端点。
package main
import (
"net/http"
"net/url"
"io/ioutil"
"fmt"
)
func main() {
query := url.Values{"jql": {"project = HSP"}}
req, err := http.NewRequest("GET", "https://your-domain.atlassian.com/rest/api/2/search?" + query.Encode(), nil)
if err != nil {
panic(err)
}
req.SetBasicAuth("[email protected]", "<api_token>")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《golang 中的 HTTPS POST 请求不如 Python 中预期的那样工作》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
