如何使用 golang 从重定向的 url 获取响应内容
How to get response content from a url which is redirected, using golang
我在 postman 中发出了一个 http post 请求,它工作正常,我还导出了 python 代码,响应数据也很好。但是当我使用带有 golang 语法的相同代码时,我无法获取响应数据。
import requests
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'X-Apple-I-FD-Client-Info': '{"U":"Mozilla/5.0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
'Content-Type': 'application/json',
'Origin': 'https://iforgot.apple.com',
'Referer': 'https://iforgot.apple.com/password/verify/appleid',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9'
}
data = '{"id":"ali@gmail.com"}'
response = requests.post('https://iforgot.apple.com/password/verify/appleid', headers=headers, data=data)
print(response.text)
print(response)
上面带有 http 请求的代码工作正常。但在 golang 中,它没有显示 python 请求所示的结果。
GoLang 代码为:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
url := "https://iforgot.apple.com/password/verify/appleid"
method := "POST"
// payload := strings.NewReader("{\"id\":\"ali@gmail.com\"}")
reqBody, err := json.Marshal(map[string]string{
"id": "ali@gmail.com",
})
if err != nil {
print(err)
}
payload := bytes.NewBuffer(reqBody)
log.Println("A Payload is ", payload)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
fmt.Println("Payload: ", payload)
req.Header.Add("Accept", "application/json, text/javascript, */*; q=0.01")
req.Header.Add("X-Apple-I-FD-Client-Info", `{"U":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36","L":"en-US","Z":"GMT+05:00","V":"1.1","F":"VGa44j1e3NlY5BSo9z4ofjb75PaK4Vpjt.gEngMQEjZr_WhXTA2s.XTVV26y8GGEDd5ihORoVyFGh8cmvSuCKzIlnY6xljQlpRD_vLwoxVagmmypZHgfLMC7AeLd7FmrpwoNN5uQ4s5uQ.gEx4xUC541jlS7spjt.gEngMQEjZr_WhXTA2s.XTVV26y8GGEDd5ihORoVyFGh8cmvSuCKzIlnY6xljQlpRD_vLwoxVagmmypZHgfLMC7Awvw0BpUMnGWqbyATblVnmccbguaDeyjaY2ftckuyPBDjaY1HGOg3ZLQ0IHxVS_509K8Qai.uJtHoqvynx9MsFyxYMA1cCyecGYiMfU.a4Y.a4Vdm_69WK2iwAxMOyeMrgPuVrA1cCtTf237lY5BSp55BNlan0Os5Apw.9o0"}`)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")
req.Header.Add("Origin", "https://iforgot.apple.com")
req.Header.Add("Referer", "https://iforgot.apple.com/password/verify/appleid")
req.Header.Add("Accept-Encoding", "gzip, deflate, br")
req.Header.Add("Accept-Language", "en-US,en;q=0.9")
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
我需要与 python.
中相同的结果
您的请求已压缩,如果您对行 req.Header.Add("Accept-Encoding", "gzip, deflate, br")
发表评论,您将收到可读的回复。
响应是
{
"appleID": {
"sstt": "DgfLAY0Pjcyptm0jAsIIjaC1gOmtrABF4SqCgY6VpT7vUEe/SGWdtgo/3hVoF0viKGnJ7iRXiW98WMJeARITg74qvsE0afVB7YscM5yr+dBOekbsV4FpUXWDrOqXageN4Jd0wu0t2870slQJZVo/pcYGVAeTg/cTQ7sNebehroX/6NB28w8nuzUTIff0hHmEpi0LdmrDwO5iyuHXQvWtEZp7+9oQa46pUDMbvqtQ5fwHkWmQgS7ItN3IK0WAqfaiPIcMESPoH4tf2xsB0Xl/qvdw9sNetY+GjjL3WbKDAOX1TZI4LW2I4KQBHOfXXxB3MoCmVdz/9MLKVuJGkmAgyxAY0koXWmrQGgiJ6fKXZWOwXoVTqMajDWDxJSOkRzVro0MXguMYEFKdwMnM"
},
"validationErrors": [
{
"code": "errorNoAppleID",
"message": "Please enter your Apple ID.",
"path": "id",
"suppressDismissal": false
}
]
}
表示apple Id缺失
您提出的请求和您使用的客户端存在根本差异。 python requests
客户端默认自动处理cookies并解压结果,而Go客户端则不会。
您还发送了一个 json 正文,但在 Go 示例中您指定正文为 application/x-www-form-urlencoded
,这意味着它不会被服务器正确解析。
如果您添加一个 jar 来处理 cookie,不请求压缩响应(或者解压缩响应),并正确设置内容类型,Go 示例将按预期工作:
url := "https://iforgot.apple.com/password/verify/appleid"
method := "POST"
jar, _ := cookiejar.New(nil)
client := &http.Client{Jar: jar}
req, err := http.NewRequest(method, url, strings.NewReader(`{"id":"ali@gmail.com"}`))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/json, text/javascript")
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
我在 postman 中发出了一个 http post 请求,它工作正常,我还导出了 python 代码,响应数据也很好。但是当我使用带有 golang 语法的相同代码时,我无法获取响应数据。
import requests
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'X-Apple-I-FD-Client-Info': '{"U":"Mozilla/5.0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
'Content-Type': 'application/json',
'Origin': 'https://iforgot.apple.com',
'Referer': 'https://iforgot.apple.com/password/verify/appleid',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9'
}
data = '{"id":"ali@gmail.com"}'
response = requests.post('https://iforgot.apple.com/password/verify/appleid', headers=headers, data=data)
print(response.text)
print(response)
上面带有 http 请求的代码工作正常。但在 golang 中,它没有显示 python 请求所示的结果。
GoLang 代码为:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
url := "https://iforgot.apple.com/password/verify/appleid"
method := "POST"
// payload := strings.NewReader("{\"id\":\"ali@gmail.com\"}")
reqBody, err := json.Marshal(map[string]string{
"id": "ali@gmail.com",
})
if err != nil {
print(err)
}
payload := bytes.NewBuffer(reqBody)
log.Println("A Payload is ", payload)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
fmt.Println("Payload: ", payload)
req.Header.Add("Accept", "application/json, text/javascript, */*; q=0.01")
req.Header.Add("X-Apple-I-FD-Client-Info", `{"U":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36","L":"en-US","Z":"GMT+05:00","V":"1.1","F":"VGa44j1e3NlY5BSo9z4ofjb75PaK4Vpjt.gEngMQEjZr_WhXTA2s.XTVV26y8GGEDd5ihORoVyFGh8cmvSuCKzIlnY6xljQlpRD_vLwoxVagmmypZHgfLMC7AeLd7FmrpwoNN5uQ4s5uQ.gEx4xUC541jlS7spjt.gEngMQEjZr_WhXTA2s.XTVV26y8GGEDd5ihORoVyFGh8cmvSuCKzIlnY6xljQlpRD_vLwoxVagmmypZHgfLMC7Awvw0BpUMnGWqbyATblVnmccbguaDeyjaY2ftckuyPBDjaY1HGOg3ZLQ0IHxVS_509K8Qai.uJtHoqvynx9MsFyxYMA1cCyecGYiMfU.a4Y.a4Vdm_69WK2iwAxMOyeMrgPuVrA1cCtTf237lY5BSp55BNlan0Os5Apw.9o0"}`)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")
req.Header.Add("Origin", "https://iforgot.apple.com")
req.Header.Add("Referer", "https://iforgot.apple.com/password/verify/appleid")
req.Header.Add("Accept-Encoding", "gzip, deflate, br")
req.Header.Add("Accept-Language", "en-US,en;q=0.9")
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
我需要与 python.
中相同的结果您的请求已压缩,如果您对行 req.Header.Add("Accept-Encoding", "gzip, deflate, br")
发表评论,您将收到可读的回复。
响应是
{
"appleID": {
"sstt": "DgfLAY0Pjcyptm0jAsIIjaC1gOmtrABF4SqCgY6VpT7vUEe/SGWdtgo/3hVoF0viKGnJ7iRXiW98WMJeARITg74qvsE0afVB7YscM5yr+dBOekbsV4FpUXWDrOqXageN4Jd0wu0t2870slQJZVo/pcYGVAeTg/cTQ7sNebehroX/6NB28w8nuzUTIff0hHmEpi0LdmrDwO5iyuHXQvWtEZp7+9oQa46pUDMbvqtQ5fwHkWmQgS7ItN3IK0WAqfaiPIcMESPoH4tf2xsB0Xl/qvdw9sNetY+GjjL3WbKDAOX1TZI4LW2I4KQBHOfXXxB3MoCmVdz/9MLKVuJGkmAgyxAY0koXWmrQGgiJ6fKXZWOwXoVTqMajDWDxJSOkRzVro0MXguMYEFKdwMnM"
},
"validationErrors": [
{
"code": "errorNoAppleID",
"message": "Please enter your Apple ID.",
"path": "id",
"suppressDismissal": false
}
]
}
表示apple Id缺失
您提出的请求和您使用的客户端存在根本差异。 python requests
客户端默认自动处理cookies并解压结果,而Go客户端则不会。
您还发送了一个 json 正文,但在 Go 示例中您指定正文为 application/x-www-form-urlencoded
,这意味着它不会被服务器正确解析。
如果您添加一个 jar 来处理 cookie,不请求压缩响应(或者解压缩响应),并正确设置内容类型,Go 示例将按预期工作:
url := "https://iforgot.apple.com/password/verify/appleid"
method := "POST"
jar, _ := cookiejar.New(nil)
client := &http.Client{Jar: jar}
req, err := http.NewRequest(method, url, strings.NewReader(`{"id":"ali@gmail.com"}`))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Accept", "application/json, text/javascript")
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))