2

I am new to golang, getting familiar with http package. I am having trouble in getting the post data I am sending using postman.

http://localhost:8084/dbTest is my URI. I am passing key: hub_id value: 1 using form-data. I tried following approaches,

req.ParseForm()
fmt.Println("hub_id", req.Form["hub_id"])
req.Form.Get("hub_id")

But none of the approach work. I am getting empty response.

Following is my code:

package main

import (
    "fmt"
    "net/http"
    "log"
) 

func dbtest(w http.ResponseWriter, req *http.Request) {
  req.ParseForm()
  fmt.Println("hub_id", req.Form["hub_id"])
  req.Form.Get("hub_id")
  fmt.Println(req.PostFormValue("hub_id")) //response is empty
}

func main() {

    http.HandleFunc("/dbTest", dbtest)

  log.Fatal(http.ListenAndServe(":8084", nil))
}

When I print req i get the following:

&{POST /dbTest HTTP/1.1 1 1 map[Origin:[chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop] Connection:[keep-alive] Content-Type:[multipart/form-data; boundary=----WebKitFormBoundarydFOTVjOJMeqOHnS3] Content-Length:[138] Accept-Language:[en-US,en;q=0.8] Cache-Control:[no-cache] Accept-Encoding:[gzip, deflate] Accept:[*/*] User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36] Postman-Token:[ac7ae3a9-60f6-2146-3f1c-209de7622774]] 0xc210012e70 138 [] false localhost:8084 map[] map[] <nil> map[] 127.0.0.1:34152 /dbTest <nil>}

Solution: I found the solution. Since, content-type is mulipart/form-data I correct way to parse form is to use req.ParseMultipartForm http method.

2
  • Please show your html template. Commented Aug 24, 2016 at 11:58
  • I am sending data via postman Commented Aug 24, 2016 at 12:06

2 Answers 2

3

You are using incorrect Content-Type. When using Content-Type:application/x-www-form-urlencoded, r.ParseForm() will parse data correctly. Check r.Form afterwards.

Sign up to request clarification or add additional context in comments.

4 Comments

Will you please explain it more. Here the content type is Content-Type:[multipart/form-data; boundary=----WebKitFormBoundarydFOTVjOJMeqOHnS3] which is send by POSTMAN.
Take a look here for explanation - stackoverflow.com/questions/4007969/… In Postman, you can define different Content-Type. In body section, below URL address you can pick different types.
yes, I got the value by using Content-Type:application/x-www-form-urlencoded but suppose in future i will have to send binary (non-alphanumeric) data (or a significantly sized payload) then multipart/form-data should be used. How to obtain POST data when content-type is multipart/form-data in go.
I found the solution. Since, content-type is mulipart/form-data ,the correct way to parse form is to use req.ParseMultipartForm http method.
0

Try

err := r.ParseForm()
v := r.Form
h := v.Get("hub_id")

Edit: I see you've tried ParseForm

5 Comments

Are you posting the data, i.e. Method="POST" from a form ?
Try adding a Methods("POST") to the handleFunc ?
err := r.ParseForm() prints <nil> v := r.Form prints map[] h := v.Get("hub_id") prints nothing
Also, when i print req.Method it prints POST. This means that it is a POST request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.