Handling JSON Post request in Go - error

go, json

Solution

You are not sending data in JSON. That’s why your Go server can’t read that. Use something like this:

$.ajax({
  url: '/api/auth.json',
  type: 'POST',
  contentType: 'application/json; charset=utf-8',
  data: JSON.stringify(data)
  // you can add callbacks as “complete”, “success”, etc.
});

I have to add that I’m not a jQuery expert and there may be shortcuts for this.

Problem

My client side javascript posts this JSON to the server side: ``` { username: 'hello', password: 'world' } ``` My Go server side code: ``` type AuthJSON struct { Username string `json:"username"` Password string `json:"password"` } func AuthenticationHandler(w http.ResponseWriter, r *http.Request) { // Parse the incoming user/pass from the request body var body AuthJSON err := json.NewDecoder(r.Body).Decode(&body) log.Println(body) log.Println(body.Username) if err != nil { log.Println(err) panic(err) } ... } ``` My terminal looks like the following: ``` 2013/07/31 20:26:53 { } 2013/07/31 20:26:53 2013/07/31 20:26:53 invalid character 'u' looking for beginning of value 2013/07/31 20:26:53 http: panic serving [::1]:58141: invalid character 'u' looking for beginning of value goroutine 9 [running]: net/http.func·007() ... ``` Obviously there is something wrong with the parsing of my JSON and I'm getting an error and panic(err) is being called. The invalid character 'u' is coming from the username part in my JSON. I've tested the client side with a Node server and it works great. Very new to Go. Any help would be great. Edited: I added, ``` log.Println(r) ``` And I am getting this additional information: ``` 2013/07/31 20:59:47 &{POST /api/auth.json HTTP/1.1 1 1 map[Accept-Encoding:[gzip, deflate] Content-Type:[application/x-www-form-urlencoded; charset=UTF-8] X-Requested-With:[XMLHttpRequest] Content-Length:[30] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:22.0) Gecko/20100101 Firefox/22.0] Accept:[*/*] Accept-Language:[en-US,en;q=0.5] Referer:[http://localhost:8080/]] 0x114adee0 30 [] false localhost:8080 map[] map[] <nil> map[] [::1]:58372 /api/auth.json <nil>} ``` Here is b, _ := ioutil.ReadAll(r.Body); log.Println(b) ``` 2013/08/01 07:48:40 [117 115 101 114 110 97 109 101 61 104 101 108 108 111 38 112 97 115 115 119 111 114 100 61 119 111 114 108 100] ``` Full client side: ``` <script type="text/x-handlebars" data-template-name="login"> {{#if loggedIn}} <p>You are already logged in!</p> {{else}} <form class="form-inline" {{action login on="submit"}}> <h2>Log In</h2> {{input value=username type="text" placeholder="Username"}} {{input value=password type="password" placeholder="Password"}} {{input class="btn" type="submit" value="Log In"}} </form> {{#if errorMessage}} <div class="alert alert-error">{{errorMessage}}</div> {{/if}} {{/if}} </script> App.LoginController = Ember.Controller.extend({ login: function() { var self = this, data = this.getProperties('username', 'password'); // Clear out any error messages. this.set('errorMessage', null); $.post('/api/auth.json', data).then(function(response){ // Check the response for the token self.set('errorMessage', response.message); if(response.success){ self.set('token', response.token); } }); } }); ```

Original source