GithubHelp home page GithubHelp logo

Comments (7)

poweroftrue avatar poweroftrue commented on August 24, 2024 6

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.

poweroftrue avatar poweroftrue commented on August 24, 2024 1

@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.

poweroftrue avatar poweroftrue commented on August 24, 2024 1

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.

objque avatar objque commented on August 24, 2024

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.

objque avatar objque commented on August 24, 2024

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.

poweroftrue avatar poweroftrue commented on August 24, 2024

@objque what is your use case ?

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

from goreq.

objque avatar objque commented on August 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.