Altenrion February 2016

golang, revel, How to parse post json?

I'm rather new in golang, but I'm trying hard..

I'm trying to send json by post request to the revel controller, and parse it on the revel side.

But while getting the result I cannot Unmarshal it... I'm sending an array

json_encode(array("one","two","three"))

But I can't find the correct way to work with such a data. I'm not sure do I need to make json before sending it or not..

func (c KpiCtrl) GetData() revel.Result {
    content, _ := ioutil.ReadAll(c.Request.Body)
    ...
    return c.RenderJson(content)
    }

returns

"WyJvbmUiLCJ0d28iLCJ0aHJlZSJd"

I tried to use json.Unmarshal but it returns errors.. What is the best practice to work with post data sent by curl to revel controller?

Answers


wlredeye February 2016

Just use standart json Decoder:

   var content []string
   err := json.NewDecoder(c.Request.Body).Decode(&content)
   if err != nil {
       log.Fatal("JSON decode error: ", err)
   }
   defer c.Request.Body.Close()
   fmt.Println(content)

Post Status

Asked in February 2016
Viewed 1,680 times
Voted 9
Answered 1 times

Search




Leave an answer