0

I have a WebAPI C# application. My GET method is defined as:

[HttpGet]
public HttpResponseMessage Get(string id)

This API retrieves some content from a database, based on a given id. Another parameter is required but it is so long that having it on the URL would not work, so I'm using the GET body to send such second parameter.

How can I retrieve it from inside the get method?

I tried

var dataOnBody = await Request.Content.ReadAsStringAsync();

but it doesn't work as the Get method is not async and I think it doesn't need to be that (I want a normal blocking function which reads the content of the body and outputs a string)

I just need a simple way to extract my string from the request body

Gianluca Ghettini
  • 9,354
  • 14
  • 63
  • 132

1 Answers1

1

Even if you somehow manage to do this, you will find that support is not universal. The HTTP specs say:

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

So the data returned relies only on the URI, not anything in the body. Many libraries won't even let you send a request body during a GET.

Gabriel Luci
  • 28,970
  • 3
  • 37
  • 58
  • Ok, so what's the best approach when a very long parameter has to be sent as part of a HTTP GET request? – Gianluca Ghettini Nov 14 '16 at 14:28
  • If it can't go in the URI, then a POST or PUT is really your best option. Or maybe web socket. But it all depends on what you're actually trying to accomplish. – Gabriel Luci Nov 14 '16 at 14:29
  • Note: this particular language was removed in later versions of the HTTP spec [RFC 7231 / 4.3.1](https://tools.ietf.org/html/rfc7231#section-4.3.1) and there in theory, there's no longer a spec limitation on GET requests with a message body. (See also, updated answer on [this answer](https://stackoverflow.com/a/983458/1424754) which also discusses this. – nicholas Feb 04 '21 at 17:08