[go] Simple POST requests

I have example code

func MakeRequest() {

formData := url.Values{
	"name": {"masnun"},
}

resp, err := http.PostForm("https://httpbin.org/post.php", formData)
if err != nil {
	log.Fatalln(err)
}

var result map[string]interface{}

json.NewDecoder(resp.Body).Decode(&result)

log.Println(result["form"])
}

What if my parameters go as a string, for example:

var url string = "https://httpbin.org/post.php"
var param string = "lastname=Voucher&company_name=Fredy&address=Alcap

How do I properly encode and send a POST request?

I think you can use url.parseQuery to get a slice form a querystring

m, _ := url.ParseQuery(array=10&array=11)
fmt.Println(m[“array”][0], m[“array”][1])

1 Like
func main() {

var s, _ = url.ParseQuery("firstname=ghbdtn rfr ltkf&email=mymail.ru&lastname=vdsvsdvvs&company_name=sdv&address=sdc&password=1234321&reg_user=1")




req, _ := http.PostForm("https://shop.vidict.net/register.php", s)
req.Header.Set("content-type", "application/x-www-form-urlencoded")





defer req.Body.Close()
}

I try this code but not work))

Hi. Use this instead
https://golang.org/pkg/net/url/#Values

Example here: https://play.golang.org/p/FtQIz_ePF_N

Thanks for the answer, in my case, the parameters are a string, I just can not right now post request to implement

Well you can convert to a slice. For example :slight_smile:

str := “firstname=ghbdtnrfrtkf&email=mymail.ru&lastname=vdsvsdvvs&company_name=sdvaddress=sdcpassword=1234321&reg_user=1”
x := strings.Split(str, “&”)
urlData := url.Values{}
for _, value := range x{
a := strings.Split(value, “=”)
urlData.Set(a[0], a[1])
}
fmt.Println(urlData)

1 Like

thanks Yamil, I try now this code

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.