JSON decoding in golang - json

So I tried something based on the example here in my code, and get no data, but no error either. The code is:
import (
"io"
"fmt"
"net/http"
"encoding/json"
)
type Credential struct {
username string `json:"username"`
password string `json:"password"`
}
func login(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
cred := Credential{}
err := json.NewDecoder(req.Body).Decode(&cred)
if err != nil {
panic("can't decode")
}
fmt.Println("credentials: " + cred.username + " , " + cred.password)
}
}
I test with
curl -X POST -H "Accept: application/json" --data "{\"username\":\"x\",\"password\":\"y\"}" 127.0.0.1:8000/login -i
And the server prints out:
credentials: ,
Why is there nothing in cred.username and cred.password?

golang use first character of the field to declare public or private for that struct. so change username to Username

Related

Post Request with strings.NewReader Containing Escape Sequence

I'm trying to retrieve a response from a POST endpoint which accepts a payload.
For curl request:
curl --request POST \
--url https://api.io/v1/oauth/token \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
"userToken": "myemail#domain.com:MyUserProfileToken"
}'
I can do this with:
func GetJWT() string {
endpoint := "https://api.io/v1/oauth/token"
payload := strings.NewReader(`{
"userToken":"myemail#domain.com:MyUserProfileToken"
}`)
req, _ := http.NewRequest("POST", endpoint, payload)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
return string(body)
}
and
payload := strings.NewReader("{\n \"userToken\": \"myemail#domain.com:MyUserProfileToken\"\n}")
However, when I try to pass string pointers for email and token, and declare the payload like
func GetJWT(userEmail, userToken *string) string {
endpoint := "https://api.io/v1/oauth/token"
payload := strings.NewReader("{\n \"userToken\": \*userEmail\":\"\*userToken\n}")
req, _ := http.NewRequest("POST", endpoint, payload)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
return string(body)
}
an error is returned for unknown escape (column 53 on payload declaration).
How can I escape string pointers so that I can concatenate userEmail, ":", and userToken
I see a couple problems here.
First: I think the "unknown escape" error message is caused by \* since \* is not a legitimate escape character.
Second: Golang does not support string interpolation. So the userEmail and userToken variables are actually never used in your GetJWT function.
You can format variables into a string using Sprintf from the standard library fmt package. That would look like this:
fmt.Sprintf("{\n \"userToken\" : \"%s:%s\" \n}", *userEmail, *userToken)

Golang how to pass username and password as JSON body to Post API

Using a post API request I want to receive a JWT token for the response.
For that, I want to send the username and password as JSON body input to the API. The username and password should be taken dynamically from the user as a JSON payload.
Currently, I am hardcoding the user name and password using strings.NewReader which is not suitable as this is a web application and many users will be using it.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
)
var authToken string
type authorization struct {
AuthorizationToken string `json:"authorizationToken"`
ExpiresTimestamp string `json:"expiresTimestamp"`
}
func main() {
enverr := godotenv.Load(".env")
if enverr != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
token()
}
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.NewReader(`
{
"name" : "testuser",
"pwd" : "testpwd",
"hName" : "hname"
}
`)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
if err != nil {
panic(err)
}
defer response.Body.Close()
content, _ := ioutil.ReadAll(response.Body)
var result authorization
if err := json.Unmarshal(content, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
}
authToken := result.AuthorizationToken
fmt.Println(PrettyPrint(authToken))
}
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
You can use http requests body (for POST request)
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
c := Credentials{}
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
err: = decoder.Decode(&c)
if err!=nil{
// handle it
}
// use it
fmt.Println(c.Username, c.Password)
If you are intending to distribute the application as an executable (aka command line in the Unix world), you can either:
Provide the username and password as program arguments, os.Args (omitting unchanged code):
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", os.Args[2], -1), "USER", os.Args[1], -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}
You should then be able to run your program (once compiled): ./filename user password
Or as better alternative, having sensitive data involved, have the username and password input from standard input with password being echoed (hiding the characters):
import (
// ...
"syscall"
"golang.org/x/term"
)
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username := reader.ReadString('\n') // handle error
fmt.Print("Enter password: ")
pwdbytes := term.ReadPassword(int(syscall.Stdin))
password := string(pwdbytes)
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", password, -1), "USER", username, -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}

json decoding/unmarshalling not working as expected in json

I have an authentication payload that I need to decode to obtain some tokens in a webapp:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJzUG9sQmV3Z2ZBMGxHbFdzTnZLNEVndGJ1WHhnOU90dVhXeFRVbWhKeF9NIn0.eyJqdGkiOiJkMjAzOWQ0ZC00NjEwLTQ2ZmMtYjE5NC03Nzc4YWJhZGE1YmYiLCJleHAiOjE1NDkyOTQyMjUsIm5iZiI6MCwiaWF0IjoxNTQ5MjkzOTI1LCJpc3MiOiJodHRwOi8vMTkyLjE2OC45OS4xMDA6OTAvYXV0aC9yZWFsbXMvc2FtcGxlc2hvcCIsImF1ZCI6ImFjY291bnQiLCJzdWIiOiJkYWU0NjE1OC03ZjQ2LTQ0N2UtOGZmMC0xNWIxNzEyZGY1NWIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJmcm9udGVuZCIsImF1dGhfdGltZSI6MTU0OTI5MzkyNSwic2Vz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mRmNTViIiwidHlwIjoiSUQiLCJhenAiOiJmcm9udGVuZCIsImF1dGhfdGltZSI6MTU0OTI5MzkyNSwic2Vzc2lvbl9zdGF0ZSI6IjY2YjNkNjJkLWIzYzYtNDJiYi04Njc5LWYwZGVkMGU3ODk1NyIsImFjciI6IjEiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsIm5hbWUiOiJ0ZXN0ZXIgdGVzdGVyIiwicHJlZmVycmVkX3VzZXJuYW1lIjoidGVzdGVyQHRlc3Rlci5jb20iLCJnaXZlbl9uYW1lIjoidGVzdGVyIiwiZmFtaWx5X25hbWUiOiJ0ZXN0ZXIiLCJlbWFpbCI6InRlc3RlckB0ZXN0ZXIuY29tIn0.kKIpxriAXWiySz9xOmr_XHmMgz7_L0q-qgt0WHb8yFRSdrPFePRqoe_RKnxtxwn6nIGOYbY78djr9GMtMpAcJquumpHpo3RGG1o5Y088rft4A7NNvUwDJeZhx6DkeVq5FfL0hvNfUntfUvtQaaVYh4M6TjJo6nakyJ1ZSyrRwSZZmNXzd0S4XY3oV19DOiyrLl3qBxhgw33pLYla5dIAs_KnrGVmLSpNJT61T810kqN-dT1c4EFMZ1iz7Bfh4RucK4oHGZmPlR3znqMMijirK8QT7ukTJvhG-TlmfsBMe0r_9UjdQ9wxjZ7pDi7pfYKVOv55O9Lzk2PbAjIHe3UKiA",
"not-before-policy": 1549293916,
"session_state": "66b3d62d-b3c6-42bb-8679-f0ded0e78957",
"scope": "openid email profile"
}
The struct I am using is as follow:
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry time.Time `json:"expires_in"`
}
After decoding the response, I tried printing the response :
var authToken Token
json.Unmarshal(response.Body, &authToken)
fmt.Println("-------------------- accessToken " + authToken.AccessToken)
fmt.Println("-------------------- refreshToken " + authToken.RefreshToken)
fmt.Println("-------------------- expires ", authToken.Expiry)
fmt.Println("-------------------- type " + authToken.TokenType)
Only the first one AccessToken has a value, everything else is empty.
I also tried using json.NewDecoder(response.Body).Decode(&authToken)
Same result.
Is there anything wrong with my approach ?
You have to check error from json.Unmarshal or from decoder.Decode.
And "expires_in": 299, it is not time, it is int.
code:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry int `json:"expires_in"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var authToken Token
if err := decoder.Decode(&authToken); err != nil {
fmt.Fprintf(w, "error: %s", err)
return
}
fmt.Println("-------------------- accessToken " + authToken.AccessToken)
fmt.Println("-------------------- refreshToken " + authToken.RefreshToken)
fmt.Println("-------------------- expires ", authToken.Expiry)
fmt.Println("-------------------- type " + authToken.TokenType)
})
http.ListenAndServe(":8000", nil)
}
curl:
curl -XPOST 'http://localhost:8000' -H 'Content-Type: application/json' -d '{
"access_token": "at",
"refresh_token": "rt",
"expires_in": 299,
"token_type": "bearer"
}'
result:
-------------------- accessToken at
-------------------- refreshToken rt
-------------------- expires 299
-------------------- type bearer

Using PUT to update several JSON data in one request

I followed this guide Developing a simple CRUD API with Go, Gin and Gorm to build my first RESTful service in golang, I can read parameter(s) from my web service, and update one value of parameter, But how to parse the JSON to update several values of parameters at one PUT request.
The CURL command I intend to use for my test as bellow
$ curl -i -X PUT http://localhost:8080/params -d '{ [ {"id":"1","value": "10"}, {"id":"2","value": "20"}] }'
Following is my code
package main
import (
"fmt"
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
)
type Param struct {
ID int `json:"id" `
Name string `json:"name"`
Title string `json:"title"`
Value string `json:"value"`
}
type ParamValue struct {
ID int `json:"id" `
// Name string `json:"name"`
// Title string `json:"title"`
Value string `json:"value"`
}
var db *gorm.DB
var err error
func main() {
db, err = gorm.Open("mysql", "user:password#tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
if err != nil {
log.Panic(err.Error())
}
db.AutoMigrate(&Param{})
r := gin.Default()
r.GET("/params", GetParams)
r.GET("/params/:id", GetParam)
r.PUT("/params/:id", UpdateParam)
r.PUT("/params", UpdateParams) // How to implement this one
r.Use(cors.Default())
r.Run()
}
func GetParams(c *gin.Context) {
var params []Param
if err := db.Find(&params).Error; err != nil {
c.AbortWithStatus(404)
} else {
c.JSON(200, params)
}
}
func GetParam(c *gin.Context) {
id := c.Params.ByName("id")
var param Param
if err := db.Where("id = ?", id).First(&param).Error; err != nil {
c.AbortWithStatus(404)
} else {
c.JSON(200, param)
}
}
func UpdateParams(c *gin.Context) {
// Debug info
fmt.Println("c.Request.Method >> " + c.Request.Method)
fmt.Println("c.Request.URL.String() >> " + c.Request.URL.String())
// command for testing
// $ curl -i -X PUT http://localhost:8080/params -d '{ [ {"id":"1","value": "10"}, {"id":"2","value": "20"}] }'
}
func UpdateParam(c *gin.Context) {
var param Param
id := c.Params.ByName("id")
if err := db.Where("id = ?", id).First(&param).Error; err != nil {
c.AbortWithStatus(404)
} else {
name := param.Name // readonly
c.BindJSON(&param)
if param.Name != name { // if changed
param.Name = name // restore the origin
}
db.Save(&param)
c.JSON(200, param)
}
// $ curl -i -X PUT http://localhost:8080/params/63 -d '{ "name": "recharge", "title": "switch 0/1:on/off", "value": "1"}'
}
Any help is appreciated.
first of all, you json is invalid.
I guess that this is what you wanted:
[{"id":"1" ,"value": "10"}, {"id":"2" ,"value": "20"}]
an easier solution will be to add an struct that match your json:
and then add the json.Unmarshal to parse the json:
jsn := `[{"id":"1" ,"value": "10"}, {"id":"2" ,"value": "20"}]`
var data yourSturctName
json.Unmarshal([]byte(jsn), &data)
for _, r := range data {
fmt.Printf("ID:%s Value:%s", r.ID, r.Value)
}
then add that into your logic.

Golang post return json response

I'm trying to make a request to my (magento)webserver using golang.
I managed to make a POST request, but however, I'm not getting the same response I'm getting when using CURL from the cmd.
I followed this post (How to get JSON response in Golang) which does not give me any response.
So I tried this
package main
import (
"fmt"
"net/http"
"os"
)
var protocol = "http://"
var baseURL = protocol + "myserver.dev"
func main() {
fmt.Print("Start API Test on: " + baseURL + "\n")
r := getCart()
fmt.Print(r)
}
func getCart() *http.Response {
resp, err := http.Post(os.ExpandEnv(baseURL+"/index.php/rest/V1/guest-carts/"), "", nil)
if err != nil {
fmt.Print(err)
}
return resp
}
This just return the http reponse like
{200 OK 200 HTTP/1.1 1 1 map[Date:[Thu, 04 May 2017 05:30:20 GMT] Content-Type:[application/json; charset=utf-8] Set-Cookie:[PHPSESSID=elklkflekrlfekrlklkl; expires=Thu, 04-May-2017 ...
When using curl -g -X POST "my.dev/index.php/rest/V1/guest-carts/" I retrieve some kind of ID which I need to proceed.
How can I get the this curl result in golang?
You need to read the resp.Body (and don't forget to close it!), ie
func main() {
fmt.Print("Start API Test on: " + baseURL + "\n")
r := getCart()
defer r.Body.Close();
if _, err := io.Copy(os.Stdout, r.Body); err != nil {
fmt.Print(err)
}
}