-2

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?

anta40
  • 6,511
  • 7
  • 46
  • 73

1 Answers1

1

You are missing a double quote after the tag message:

type Response struct {
    Message string `json:"message`
    Data    string `json:"data"`
}
Clément
  • 804
  • 6
  • 16