Answer the question
In order to leave comments, you need to log in
How to rewrite request code from python 2 to golang?
There is no way to rewrite this code in golang. Source:
# -*- coding: utf-8 -*-
import urllib
import json
data['param1'] = '1'
data['param2'] = '2'
params = {}
params['access_token'] = '3e17b2be0282a44'
params['data'] = json.dumps(data)
params = urllib.urlencode(params)
f = urllib.urlopen("http://pbrf.ru/pdf.F7", params)
print f.read()
func Main() {
req, err := http.NewRequest("GET", "http://pbrf.ru/pdf.F7", nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
q := req.URL.Query()
addKeyAndValue(&q, Init2())
req.URL.RawQuery = q.Encode()
fmt.Println(req.URL.String())
client := &http.Client{}
req.Header.Add("Content-Type","application/x-www-form-urlencoded")
resp, err := client.Do(req)
checkErr(err);
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
}
func addKeyAndValue(query *url.Values, parameters map[string]string) {
for key, value := range parameters {
query.Add(key, value)
}
return
}
func NewBlank() {
type Request struct {
Access_token string `json:"access_token"`
Data map[string]string `json:"data"`
}
u := Request{ "3e17b2be0282a44505",Init()}
req, err := json.Marshal(u)
checkErr(err)
body := bytes.NewReader(req)
fmt.Println(string(req))
res, err := http.Post("http://pbrf.ru/pdf.F7", "application/json; charset=utf-8", body)
checkErr(err)
bod, err := ioutil.ReadAll(res.Body)
checkErr(err)
msg := gjson.Get(string(bod), "message").String()
println(msg)
}
Answer the question
In order to leave comments, you need to log in
The request must be POST.
Headers - application/x-www-form-urlencoded
You can check what is actually being sent by replacing the address with https://httpbin.org/post
Go example below
func Init() string {
res := make(map[string]string)
res["param1"] = "1"
res["param2"] = "2"
data, err := json.Marshal(res)
checkErr(err)
return string(data)
}
func NewBlank() {
address := "http://pbrf.ru/pdf.F7"
fmt.Println("URL:>", address)
payload := url.Values{"access_token": []string{"3e17b2be0282a44505"}, "data": []string{Init()}}
req, err := http.NewRequest("POST", address, bytes.NewBuffer([]byte(payload.Encode())))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
checkErr(err)
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question