如何从使用golang重定向的URL获取响应内容

问题描述 投票:1回答:1

我在邮递员中发出了一个http发布请求,并且工作正常,而且我导出了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":"[email protected]"}'


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\":\"[email protected]\"}")

    reqBody, err := json.Marshal(map[string]string{
        "id": "[email protected]",
    })
    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中相同的结果。

python go post postman net-http
1个回答
1
投票

您的请求已压缩,如果您对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
        }
    ]
}

表明缺少苹果ID

© www.soinside.com 2019 - 2024. All rights reserved.