I have made a http post request in postman and It is working fine, and also I export the python code and the response data is fine. But when I use the same code with golang syntax I am unable to get the response data.
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)
The Above code with http request is working fine. But in golang it is not showing the result as shown by python request.
GoLang code is:
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))
}
I need the same results that i get in the python.
There are fundamental differences in both the request you are making, and the clients you are using. The python requests
client is automatically handling cookies and decompressing the result by default, while the Go client does not.
You are also sending a json body, but in the Go example you specify that the body is application/x-www-form-urlencoded
, which means it's not going to be correctly parsed by the server.
If you add a jar to handle cookies, don't request a compressed response (or alternatively decompress the response), and correctly set the content type, the Go example works as expected:
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))