http获取 - 带有身体和URI Parms- [来自Body]变量为null
我有一个已作为帖子实现的端点。它实际上是基础的,实际上可能是 /应该是一个问题,但是由于所需的数据包括集合或列表,所以我将其更改为带有JSON主体的帖子。
但是团队发现这令人困惑。因此,现在我被要求用URI参数和人体列表作为JSON来实现。
因此,从本质上讲,我需要做的就是这样的事情:
GET http://localhost:123/locations/{locationID}/products/{productID}/stats/{dateRange}
Body: {"count":["exports","imports","anothertiem", "thisListCanbeLong"]}
我试图更改C#功能签名以使其看起来像这样:
[HttpGet]
[Route("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
[AllowAnonymous]
public ProductStats GetStats(ProductStatRequest request, [FromBody] List<string> typesofStats)
{
然后,当我尝试使用这种类型的请求使用人才或邮递员来调用端点时:
http://localhost:123/locations/4/products/all/stats/15
headers:headers:
Content-Type header is set to "application/json"
主体
{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}
通话失败了TypeOfStats列表为null。不确定如何在这种类型的端点中提取身体。
另外,虽然我确实读到可以处理身体,但这也许不是最好的方法。也许帖子更好,我只需要回到团队中说不,或者帖子可以同时处理Uri Parms和身体。 团队的问题是,他们希望在URI中看到通过的位置ID和产品ID等 - 使其与其余的呼叫保持一致。
感谢您的帮助。
I have an endpoint that has been implemented as a POST. It's actually idemponent and really could be / should be a GET but because the data required includes a collection or a list, I changed it to a POST with a json body.
But the team finds this confusing. So now I'm being asked to implement as a GET with both URI parameters and a list in the body as JSON.
So essentially what I need to do is something like this:
GET http://localhost:123/locations/{locationID}/products/{productID}/stats/{dateRange}
Body: {"count":["exports","imports","anothertiem", "thisListCanbeLong"]}
I've tried to change my c# function signature to look like this:
[HttpGet]
[Route("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
[AllowAnonymous]
public ProductStats GetStats(ProductStatRequest request, [FromBody] List<string> typesofStats)
{
And then when I try to call the endpoint using either Talent or Postman using this type of a request:
http://localhost:123/locations/4/products/all/stats/15
Headers:
Content-Type header is set to "application/json"
Body
{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}
the call fails the typeofStats list is null. Not sure how I can extract the body in this type of an endpoint.
Also, while I did read that GET can handle body, maybe this isn't the best approach. Maybe the POST is better and I just have to get back to the team and say no, or maybe the POST can handle both URI parms and body.
The issue for the team is that they want to see in the uri the location id being passed and the product id etc - to keep it uniform with the rest of the calls.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET MVC框架可与命名约定一起使用,这意味着参数名称必须匹配请求数据名称。随着以下更改应起作用:
...以及Postman的请求/响应
如果您想在请求正文中提供一个列表中的对象,则应期望相同的对象在动作方法中。
应该期待:
The Asp.net MVC framework works with naming conventions, which means that the parameter names must match the request data names. With the following changes should work:
... and the request/response from postman
If you want to provide an object with a list in it in the body of the request, you should expect the same object in the action method.
should be expected as: