5

How to send a Complex Object( Which may have huge data) to a Web API Get Method? I know I can user 'FromUri' option, but since the data is too large I can't use that option. I want to user 'FromBody' option. But can I post data to a Get Method?????

Please guide me here...thanks in advance

3 Answers 3

18

How to send a Complex Object( Which may have huge data) to a Web API Get Method?

You can't. GET methods do not have request body. You have to send everything in the query string which has limitations.

You should use POST instead.

Sign up to request clarification or add additional context in comments.

3 Comments

So In this case..."WCF" is better option compared to "WebAPI". Because posting huge data in GET might be not a good idea??
@KokiralaSudheer, you cannot post data in GET :-) You are contradicting yourself. WCF won't help you at all. I would recommend you reading the HTTP protocol specification and familiarize yourself with the GET and POST verbs and most importantly with their differences. When you use the GET verb, the request doesn't have any body. You can only send values to the server with query string parameters. If you need to send huge data to the server you should use POST.
@KokiralaSudheer Think about it like this: GET:provided a URL, will get some data. POST:provided a URL and some data, will send some data and get some data in return.
2

You need to create a method similar to the below:

[HttpPost]
public HttpResponseMessage MyMethod([FromBody] MyComplexObject body)
{
    try
    {
        // Do stuff with 'body'
        myService.Process(body);
    }
    catch (MyCustomException)
    {
        return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("FAILED") };
    }

    return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("OK") };
}

If your POSTing data larger than 4MB, then you'll need to adjust your web.config to configure IIS to accept more, the below example sets the maxRequestLength and maxAllowedContentLength to 10MB:

<system.web>
    <httpRuntime maxRequestLength="10240" />
</system.web>

and

<system.webServer> 
      <security> 
          <requestFiltering> 
             <requestLimits maxAllowedContentLength="10485760" /> 
          </requestFiltering> 
      </security> 
</system.webServer>

3 Comments

So In this case..."WCF" is better option compared to "WebAPI". Because posting huge data in GET might be not a good idea??
You can't POST data using a HTTP GET, you are constrained to the URI which is usually less than 2000 characters. If you need to POST a large amount of data to a service endpoint you should do so using HTTP POST and place the data inside the body.
I wouldn't compare WCF or Web API like that. Each has its proper application.
0

use OData maybe its fine for not too big objects

https://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

Or use FromUri like below

public MethodName Get([FromUri]Model model, int page, int pageSize)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.