.net REST API:带有正文的 GET 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11091160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:29:31  来源:igfitidea点击:

REST API: GET request with body

.netrestasp.net-web-apihttp-getfrombodyattribute

提问by user437899

I want to implement a REST API and need a body on my GET requests. (Like discussed here: HTTP GET with request body)

我想实现一个 REST API 并且需要一个关于我的 GET 请求的正文。(就像这里讨论的:HTTP GET with request body

Are there http clients which are not able to send a body with a GET request? Fiddler is able to do it, although the message box is red.

是否有无法通过 GET 请求发送正文的 http 客户端?Fiddler 能够做到这一点,尽管消息框是红色的。

回答by Ben

As a general rule, the idea of a GET in REST is that any of your parameters are sent in the URL. As the answer on the question you included indicates, it's doable but misses the point of REST, which is to have a consistent webbish interface. If you want to pass complex data to your endpoint, you probably want to use a POST, which your users will expect to have a body for. I'd highly recommend reconsidering that implementation.

作为一般规则,REST 中 GET 的想法是您的任何参数都在 URL 中发送。正如您所包含的问题的答案所表明的那样,它是可行的,但忽略了 REST 的要点,即具有一致的 webbish 界面。如果您想将复杂的数据传递到您的端点,您可能需要使用 POST,您的用户希望有一个正文。我强烈建议重新考虑该实施。

But to your actual question, sure there are clients that can't send a body on a GET. Mostly I'd imagine your clients will be programmatic, say, python's urlib2, and while you canset a body on GET, it is not really the intended use of the module, so you're forcing the programmer to get weird. More importantly, the idea of the REST api is to be client-agnostic, which is why it sounds to me like your API design should be reworked here.

但是对于您的实际问题,请确保有些客户无法在 GET 上发送正文。大多数情况下,我认为您的客户将是程序化的,例如,python 的 urlib2,虽然您可以在 GET 上设置主体,但这并不是该模块的真正用途,因此您迫使程序员变得奇怪。更重要的是,REST api 的想法是与客户端无关,这就是为什么我觉得你的 API 设计应该在这里重新设计。

回答by Regfor

It's a bad idea to use body in GET HTTP requests. Yes, seems to be that "de jure" HTTP GET can have body, but "de facto" you will be have problems:

在 GET HTTP 请求中使用 body 是个坏主意。是的,似乎“法律上的”HTTP GET 可以有正文,但“事实上”您会遇到问题:

  1. With client frameworks/libraries. It will be hard to find support of it.
  2. Server can just ignore the body of GET Request. And anyway it's not standard way, and could be problems with server or it's configuration.
  3. It makes your code, especially on server side, unclear to others, because nobody will expect GET with body.
  1. 使用客户端框架/库。很难找到它的支持。
  2. 服务器可以忽略 GET 请求的主体。无论如何,这不是标准方式,可能是服务器或其配置的问题。
  3. 它使您的代码,尤其是在服务器端,对其他人来说不清楚,因为没有人会期望 GET 带有 body。

Are you looking for hard way? With GET with body you will got so many pitfalls. Why not to use other HTTP Verb?

你在寻找艰难的道路吗?使用 GET with body 你会遇到很多陷阱。为什么不使用其他 HTTP Verb?

For example use POST (or some other verbs), than:

例如使用 POST(或其他一些动词),而不是:

  1. it's easy to have already ready client library,
  2. no problems with server or server configuration,
  3. it's clear to other
  1. 已经准备好的客户端库很容易,
  2. 服务器或服务器配置没有问题,
  3. 其他人很清楚

Do not look for harder ways :)

不要寻找更难的方法:)