This repository has been archived by the owner on Aug 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
How to send POST application/x-www-form-urlencoded (NOT JSON) #135
Comments
@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"
}
|
Hi, poweroftrue! |
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)
} |
Yeah. |
@objque what is your use case ? I mean from where you will get the string you want to send with request ? |
Server can accept So i want to create map and simply send it to the server with a goreq lib. |
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.
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.
The text was updated successfully, but these errors were encountered: