Comments (7)
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":
from goreq.
@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"
}
from goreq.
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)
}
from goreq.
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
from goreq.
Yeah.
So, i need to manually convert map[string]string to string and after that send request? I hoped library supports this action. ;(
from goreq.
@objque what is your use case ?
I mean from where you will get the string you want to send with request ?
from goreq.
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.
from goreq.
Related Issues (20)
- Request Query Strings are normalized to small letters HOT 3
- Support for Custom Transport? HOT 2
- Add a DoByClient method
- json not being passed to request HOT 1
- missing license HOT 1
- Concurrency issue: call .Do() in different goroutines replace CheckRedirect of singleton client HOT 1
- using goreq with appengine HOT 1
- when assign client cookiejar not attached
- Individual debug flags for header and body?
- DNS resolve time out.
- how to post json to sever
- How to request https via proxy
- FromJsonTo() doesn't work
- Empty URI in response HOT 1
- Timeout unit tests fail
- unknown http.Transport field 'ProxyConnectHeader' in struct literal HOT 4
- can't call FromJsonTo after ToString() method has been called
- Possible for a tag/release of the "stable"
- 301 302 go func(){}() error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goreq.