英文:
Returning IDs from a POST
问题
我已经在AngularJS中构建了一个Web应用程序,并使用Go和App Engine构建了一个服务层,但是我遇到了一个问题。Angular似乎无法识别HTTP状态201(已创建)以及它填充的Location头部,而我一直在使用的gorest库在POST或PUT请求的201创建响应中除了头部之外不会返回任何正文数据。
我希望能够在我的应用程序中向集合中添加一个新项,并让服务器响应其唯一ID。
我可以在POST请求返回后进行单独的GET请求,但这样效率低且混乱。我也可以修复gorest代码,以允许在非GET动词的响应中包含正文,但可能会违反惯例标准。
我是否漏掉了什么?
英文:
I've built a web app in angularjs and a service layer using Go and App Engine but I've run into a snag. Angular doesn't seem to recognize the HTTP status 201 (Created) nor the Location header it populates and the gorest library that I've been using up until now will not return any body data for a POST or PUT except for the header on a 201 Create response.
I want to be able to add a new item to a collection in my app and have the server respond with the unique ID for it.
I could make a separate GET request after the POST comes back but that's inefficient and messy. I could also fix the gorest code to allow a body in a response for a non GET verb but it might be that I'm breaking idiomatic standards.
Is there anything I'm missing here?
答案1
得分: 1
在angular.js的问题跟踪器上有一个已关闭的问题,他们对那个功能似乎不太关心。
英文:
There's a closed issue on angular.js's issue tracker, they pretty much don't care about that feature.
答案2
得分: 0
看起来我漏掉了一些东西。
gorest库会自动将服务方法的返回类型进行封装,只要它是一个GET请求。但是它不允许使用POST或PUT方式返回数据。
然而,我注意到WriteAndOveride方法可以在任何类型的调用中设置请求体:
serv.ResponseBuilder().SetResponseCode(200).WriteAndOveride([]byte(responseBody))
虽然这不是最干净的解决方案,但它肯定可以解决我的问题。
英文:
It looks like I was missing something.
The gorest library automatically marshals the return type from a service method as long as it is a GET. It doesn't allow data to be returned this way with either a POST or PUT.
However I noticed the WriteAndOveride method could allow the body to be set on any type of call:
serv.ResponseBuilder().SetResponseCode(200).WriteAndOveride([]byte(responseBody))
Whilst it doesn't feel like the cleanest solution, it should certainly unblock me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论