doushuo1989 2016-08-31 18:51
浏览 50
已采纳

使用Iris-go处理身体位置

I'm new in Golang, I've been doing some code tests to build an REST API using Iris framework, I'm trying to get the body data from a Post to my API but I cannot make it works, I did read the Body binder http://iris-go.com/body_binder/ and followed the examples. The result I get is an empty structure:

My code:

package main

import (
  "github.com/kataras/iris"
  "fmt"
)

type PostAPI struct {
 *iris.Context
}

type Lead struct {
 fbId string
 email string
 telefono string
 version string
 mac string
 os string
}

func (p PostAPI)  Post(){

  lead := Lead{}
  err := p.ReadJSON(&lead)

  if (err != nil) {

    fmt.Println("Error on reading form: " + err.Error())
    return
  }
  fmt.Printf("Post! %v", lead)
}

func main() {

  iris.API("/", PostAPI{})
  iris.Listen(":8080")
}

The post:

curl -H "Content-Type: application/json" -X POST -d '{"fbId": "werwer","email": "werwer@gmail.com","telefono": "5555555555","version": "123","mac": "3j:3j:3j:3j","os": "uno bien chido"}' http://0.0.0.0:8080/

The result:

Post! {     }

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsf1222 2016-08-31 20:37
    关注

    You should try to export fields with json tags in your structure i.e.

     type Lead struct {
      FbId string `json:"fbId"`
      Email string `json:"email"`
      Telefono string `json:"telefono"`
      Version string `json:"version"`
      Mac string `json:"mac"`
      Os string `json:"os"`
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆