Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Get Method with Complex Object as Parameter

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

like image 257
Kokirala Sudheer Avatar asked Sep 27 '13 11:09

Kokirala Sudheer


People also ask

How do I pass complex objects to Web API?

Best way to pass multiple complex object to webapi services is by using tuple other than dynamic, json string, custom class. No need to serialize and deserialize passing object while using tuple. If you want to send more than seven complex object create internal tuple object for last tuple argument.

How do I pass a class object as parameter in Web API?

Show activity on this post. MakeUser method in the User controller for creating a username and password. UserParameter class for sending the parameters as an object. RunAsync method in console client.

How do you force a Web API to read a complex type from the URI?

To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter. The following example defines a GeoPoint type, along with a controller method that gets the GeoPoint from the URI.


2 Answers

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.

like image 74
Darin Dimitrov Avatar answered Oct 21 '22 08:10

Darin Dimitrov


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>
like image 35
Tom Hall Avatar answered Oct 21 '22 08:10

Tom Hall