Why not all key from Revel JSON response are capitalized?

140 views Asked by At

I'm trying to understand how to implement POST request using Revel framework.

enter image description here

models/login.go

package models

type LoginParam struct {
    Username string `form:"username" json:"username"`
    Password string `form:"password" json:"password"`
}

type Response struct {
    Message string `json:"message`
    Data    string `json:"data"`
}

app/controllers/login.go

package controllers

import (
    "mytestapi/app/models"
    "encoding/json"
    "log"
    "strconv"

    "github.com/revel/revel"
)

type Login struct {
    *revel.Controller
}

func (c Login) DoLogin() revel.Result {
    var login models.LoginParam
    var res models.Response

    err := json.NewDecoder(c.Request.GetBody()).Decode(&login)

    if err != nil {
        log.Fatal("JSON decode error: ", err)
    }

    res.Message = "OK"
    res.Data = login.Username + " " + login.Password

    defer c.Request.Destroy()
    return c.RenderJSON(res)
}

Postman gives this output:

{ "Message": "OK", "data": "foo barzzzz" }

Almost correct. On the JSON output, Message instead of message is printed. Why Message is capitalized, and data is not?

1

There are 1 answers

0
Clément On

You are missing a double quote after the tag message:

type Response struct {
    Message string `json:"message`
    Data    string `json:"data"`
}