Accessing POST params in golang

Hi All

Let me just post my code around here to begin with–

type Userprofile struct {
  First       string `bson:"first" json:"first"`
  Last        string `bson:"last" json:"last"`
  About       string `bson:"about" json:"about"`
}

func RegisterUser(w http.ResponseWriter, r *http.Request){
  url := r.URL.String()
  log.Println(url)
  var profile Userprofile
  decoder := json.NewDecoder(r.Body)
   decoder.Decode(&profile)
  body,err := ioutil.ReadAll(io.LimitReader(r.Body,1048576))
  if err!=nil{
    log.Println("Error reading input json")
    panic(err)
  }
  if err := r.Body.Close(); err != nil {
        panic(err)
  }
  if err := json.Unmarshal(body, &profile); err != nil {
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    w.WriteHeader(422) // unprocessable entity
    if err := json.NewEncoder(w).Encode(err); err != nil {
              panic(err)
            }
  }
  log.Println(profile.Last)
}

When I try Postman with a POST request like –
http://localhost:3002/api/profile/create?First=Sandy&Last=Man&About=Coder

I end up getting empty/no content in the body part and as such the profile.Last ends up being empty. So I wanted to check the url received and as seen above printed it out to obtain it properly as sent. So my question is –
Why is it that I am not able to parse the params of POST with Unmarshal or NewDecoder? Is it that I need to handle it by parsing url instead of r.Body?
NOTE–
I tried r.Formvalue() data:=r.Form.Get("Last")
This didn’t yield me any success either.

Thanks in advance.

Is it that I need to handle it by parsing url instead of r.Body?

Yes, all the parameters are in the URL, so your code would need to parse this URL.

@christophberger is correct, to further this there are many library’s that can help decode this into a struct for you, here is one I created https://github.com/go-playground/form

r.ParseForm()

// r.Form will include any posted values + query params
// see README for full example
decoder.Decode(&profile, r.Form)
// or
decoder.Decode(&profile, r.URL.Query())

I hope this can help you :slight_smile:

2 Likes

Thanks a lot. I thought I could handle that by accessing the body of POST request.

Thanks a lot for the help…:slight_smile:

Hi All

Just wanted to update as to how I solved it in case it would help somebody later.

As said above the approach can be of two ways I have realised.

  1. Either send POST request with all the data in the params.
  2. Send POST request with data in the body of the POST request.
    This you can see in the edit and send option in Mozilla or any browser. This way it is embed in the body of the request. If done this way,
    decoder := json.NewDecoder(r.Body)
    decoder.Decode(&yourstructurevariablehere) method works out well. It does the decoding properly into a structure.

I have taken the second approach. Thanks a lot for your inputs. :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.