Handle body post using Iris-go

时间:2016-08-31 18:51:36

标签: go go-iris

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 个答案:

答案 0 :(得分:0)

您应该尝试在结构中导出带有json标记的字段 即。

var vm = {
  t: ko.observable('modal-choice'),
  changeTemplate: (t) => () => vm.t(t)
};

ko.applyBindings(vm);