Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created August 19, 2014 13:28
Show Gist options
  • Save andreagrandi/97263aaf7f9344d3ffe6 to your computer and use it in GitHub Desktop.
Save andreagrandi/97263aaf7f9344d3ffe6 to your computer and use it in GitHub Desktop.
Parse a JSON http POST in GoLang
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type test_struct struct {
Test string
}
func parseGhPost(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var t test_struct
err := decoder.Decode(&t)
if err != nil {
panic(err)
}
fmt.Println(t.Test)
}
func main() {
http.HandleFunc("/", parseGhPost)
http.ListenAndServe(":8080", nil)
}
@CheenaT
Copy link

CheenaT commented Dec 8, 2020

invalid character 't' looking for beginning of object key string

I fixed it by replacing first and last double quotes to single quotes: -d "{"test": "that"}" -> -d '{"test": "that"}'

@omankame
Copy link

omankame commented Feb 4, 2021

Thank you. It helped me to resolved issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment