doyz51819 2014-04-08 16:41
浏览 10
已采纳

如何从goREST POST端点返回消息?

I am using an endpoint similar to the below code

type TestService struct {
    gorest.RestService `root:"/test/"`
    testPost    gorest.EndPoint `method:"POST" path:"/testPost/" postdata:"map[string]string"`
}

I get and handle the posted data, and want to return a payload to the calling client, i.e. success/error and a message.

I cannot make out from the documentation how to achieve this, appending the output tag and returning anything from the function as per a GET end point fails with the following error;

Parameter list not matching. No matching Method found for EndPoint:[testPost],type:[POST] . Expecting: #func(serv DocumentService) TestPost(PostData map[string]string)# with no return parameters.

Is it possible to return a payload to the client? If not can I redirect to a different end point on success / failure?

  • 写回答

1条回答 默认 最新

  • douzhi1924 2014-04-10 20:08
    关注

    So I found a way in case anyone runs into a similar issue.

    It seems you can write directly using an instance of the ResponseBuilder, so I create a new response builder, set the content type to json and write my response out!

    func(serv DocumentService) TestPost(PostData map[string]string){
        fmt.Printf("%+v", PostData)
        rb := serv.ResponseBuilder()
        rb.AddHeader("Content-Type", "application/json")
        rb.Write([]byte("{type:'info', msg:'Success!'}"))
    }
    

    Co-incidentally to provide output via a different method you use something like;

    serv.ResponseBuilder().Created("http://localhost:8787/orders-service/items/"+string(i.Id))
    

    Which I found at http://code.google.com/p/gorest/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata