Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

How to send POST application/x-www-form-urlencoded (NOT JSON) #135

Open
objque opened this issue Dec 23, 2016 · 7 comments
Open

How to send POST application/x-www-form-urlencoded (NOT JSON) #135

objque opened this issue Dec 23, 2016 · 7 comments

Comments

@objque
Copy link

objque commented Dec 23, 2016

Hi!

I can't understand how to send POST REQUEST from map[string]string or custom type
The goreq supports only json body requests?

Thanks.

@poweroftrue
Copy link

poweroftrue commented Jan 18, 2017

@objque do you mean post with map? sure you can:

package main

import (
	"fmt"
	"github.com/franela/goreq"
)
func main() {
	req, _ := goreq.Request{
		Uri:         "https://httpbin.org/post",
		Method:      "POST",
		Body:        map[string]string{"mostafa": "dahab"},
		ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
	}.Do()
	html, _ := req.Body.ToString()
	fmt.Println(html)
}

results:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "{\"mostafa\":\"dahab\"}": ""
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "19", 
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": null, 
  "origin": "#", 
  "url": "https://httpbin.org/post"
}

@objque
Copy link
Author

objque commented Jan 18, 2017

Hi, poweroftrue!
Thanks for your answer, but i mean something like this: when i send a map, server will receive json-body like
{"some":"value"}. but i want some=value

@poweroftrue
Copy link

like this ?

package main

import (
	"fmt"
	"github.com/franela/goreq"
)

func main() {
	req, _ := goreq.Request{
		Uri:         "https://httpbin.org/post",
		Method:      "POST",
		Body:        "mostafa=dahab",
		ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
	}.Do()
	html, _ := req.Body.ToString()
	fmt.Println(html)
}

@objque
Copy link
Author

objque commented Jan 18, 2017

Yeah.
So, i need to manually convert map[string]string to string and after that send request? I hoped library supports this action. ;(

@poweroftrue
Copy link

poweroftrue commented Jan 18, 2017

@objque what is your use case ?

I mean from where you will get the string you want to send with request ?

@objque
Copy link
Author

objque commented Jan 18, 2017

Server can accept application/x-www-form-urlencoded body. Not json
field1=value field2=another-value

So i want to create map and simply send it to the server with a goreq lib.

@poweroftrue
Copy link

the best option ever is url.Values simple and it will do necessary encoding/escape:

package main

import (
	"fmt"
	"github.com/franela/goreq"
	"net/url" //don't forget to import net/url
)

func main() {
	values := url.Values{}
	values.Add("key", "value")
        values.Add("mostafa","dahab")
	req, _ := goreq.Request{
		Uri:         "https://httpbin.org/post",
		Method:      "POST",
		Body:        values.Encode(),
		ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
	}.Do()
	html, _ := req.Body.ToString()
	fmt.Println(html)
}

results :

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "key": "value", 
    "mostafa": "dahab"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/1.1"
  }, 
  "json": null, 
  "origin": "#", 
  "url":

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants