411 Length Required
The request did not specify the length of its content, which is required by the requested resource.
I have the following code:
[HttpPost]
[Route("UploadFileAsync/{RequestID}")]
public async Task<HttpResponseMessage> UploadFileAsync(int RequestID)
{
SetUser();
long maxAllowedFileSize = 9999999;
long? contentLenght = Request.Content.Headers.ContentLength;
if (!contentLenght.HasValue || contentLenght.Value > maxAllowedFileSize)
{
return Request.CreateErrorResponse(HttpStatusCode.LengthRequired, "Please set content lenght and file size should be less than 10 Mb");
}
It works and return 411 status code when size of request is more than 9999999.
But I would like to validate it before uploading the whole request to server (as I understand, sense of this 411 status code to prevent uploading big files if server can't process it). How can I reject request and send 411 status code before sending the whole request to server?
If you want to to validate the size before sending the request to Web API, then you need to do it at Web API client level.
However if you want to perform the validation before the Action in your web api controller is executed, you can use Action Filters. Typically, following steps are involved.
Create custom action filter for the Web API by inherting ActionFilterAttribute class.
Override OnActionExecuting method and write the code to check the content length and return appropriate error code within the method definition.
Register the custom filter in WebApiConfig file.
Decorate the action for which you want to apply this filter with your custom attribute
Refer to this link for step by step implementation.
Related
I'm having trouble getting the Web API 2 attribute routing to work.
I've been trying everything I could find this whole evening but I can't find the problem.
What I want to achieve is the following:
Make a POST request to http://localhost:xxxx/api/chat/joingroup/1234 to get to the following API call:
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
This keeps getting me a http 400 message.
{"message":"No HTTP resource was found that matches the request URI 'http://localhost:41021/api/chat/joingroup/123'.",
"messageDetail":"No action was found on the controller 'Chat' that matches the request."}
But sending a post to: http://localhost:41021/api/chat/sendmessage/pm/123123 and also to http://localhost:41021/api/chat/joingroup gives me a 200
The chatcontroller:
[RoutePrefix("api/chat")]
public class ChatController : ApiController
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
[...]
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
HTTP POSTS to http://localhost:xxxx/api/chat/sendmessage are working fine.
I cannot figure out why it isn't going to the correct method when I'm calling a POST on http://localhost:xxxx/api/chat/joingroup/1234.
SOLUTION:
The solution was to reference both values that are needed in the JoinGroup method, id and connectionID. Now the request will hit this method.
Using:
http://localhost:xxxx/api/chat/joingroup/john?connectionid=123 will work.
I noticed two things on the code you sent through:
the path you POST to is: localhost:xxxx/joingroup/1234 , this
should be localhost:xxxx/api/chat/joingroup/1234
because you have 2 parameters for the joingroup, you will need to pass both of them through, may be like this localhost:xxxx/api/chat/joingroup/1234?connectionID=value or you can pass it on the request body
if the connectionID is optional you can modify the method to use option al parameters like this
public string JoinGroup(string id, string connectionID = "")
please let me know if this helps.
Thanks
Ashraf
I assume the connectionID parameter references the POSTed data. The easiest thing to make it work is to decorate it with the [FromBody] attribute and put an = in front of the value being sent like this: =MyConnection1.
Web API expects an object with properties or an array otherwise. Alternatively, you can wrap the connection ID with a custom class and pass it serialized as JSON/XML.
I'm working on an Ember.js app. I have an update function, part of an ObjectController.
The function should save my updated model, however when I call save(); it sends a POST request not a PUT request. (Tested in Chrome.)
Why would that happen? How can I make sure a PUT request is sent for updates?
Here is my code:
customer = this.get('model');
customer.set('name', 'New name');
customer.save();
For extra reference, when I log the "dirtyType" with console.log( customer.get('dirtyType') ); it says "updated".
Any help very much appreciated!
UPDATE
I've adjusted the sample code above to make it clearer, I am NOT creating a new model and wanting to use PUT. I have an existing model that I need to update.
I'm not sure if your workaround is correct in the land of PUT vs POST.
TL;DR PUT should define the resource (by Request-URI), but we don't do that during creation, so we shouldn't be using a POST. Override the create/save if you need this for your server, instead of hacking the isNew property, which may come back to bite you.
Put
9.6 PUT
The PUT method requests that the enclosed entity be stored under the
supplied Request-URI. If the Request-URI refers to an already
existing resource, the enclosed entity SHOULD be considered as a
modified version of the one residing on the origin server. If the
Request-URI does not point to an existing resource, and that URI is
capable of being defined as a new resource by the requesting user
agent, the origin server can create the resource with that URI. If a
new resource is created, the origin server MUST inform the user agent
via the 201 (Created) response. If an existing resource is modified,
either the 200 (OK) or 204 (No Content) response codes SHOULD be sent
to indicate successful completion of the request. If the resource
could not be created or modified with the Request-URI, an appropriate
error response SHOULD be given that reflects the nature of the
problem. The recipient of the entity MUST NOT ignore any Content-*
(e.g. Content-Range) headers that it does not understand or implement
and MUST return a 501 (Not Implemented) response in such cases.
If the request passes through a cache and the Request-URI identifies
one or more currently cached entities, those entries SHOULD be
treated as stale. Responses to this method are not cacheable.
The fundamental difference between the POST and PUT requests is
reflected in the different meaning of the Request-URI. The URI in a
POST request identifies the resource that will handle the enclosed
entity. That resource might be a data-accepting process, a gateway to
some other protocol, or a separate entity that accepts annotations.
In contrast, the URI in a PUT request identifies the entity enclosed
with the request -- the user agent knows what URI is intended and the
server MUST NOT attempt to apply the request to some other resource.
If the server desires that the request be applied to a different URI,
Custom Adapter
App.ApplicationAdapter = DS.RESTAdapter.extend({
createRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record, { includeId: true });
//return this.ajax(this.buildURL(type.typeKey), "POST", { data: data });
return this.ajax(this.buildURL(type.typeKey), "PUT", { data: data });
},
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var id = get(record, 'id');
// you could do the same here, but it's even more incorrect
return this.ajax(this.buildURL(type.typeKey, id), "PUT", { data: data });
},
});
http://www.ietf.org/rfc/rfc2616.txt
Thank you for all of your help guys, however I have found the issue and it is ridiculously silly.
The API I have been using had a new flag "is_new" and that had been added to the model and was overwriting the "isNew" property.
Causing Ember (and me) to get very confused.
I've tweaked the API and all is good in the world!
If the model was created with createRecord, and thus has isNew == true and you call save() the expected behavior is POST. Once the record has been persisted, and it is changed, and thus isDirty == true but isNew == false then the save() will be a PUT.
This is described in the Models Guide.
I want to call an web api method, this is how my web api method looks like:
[HttpPost]
[Route("PostMyService/updatedBy/{updatedByID}/myobjname")]
public void PostTerminalService([FromBody] List<SomeType> lstSomeObj, MyClass myobj, int updatedByID)
{
//Do Something
}
This is how my client looks like:
int loggedinuserid=1;
List<Sometype> liTS = new List<SomeType>();
MyClass myobj=new MyClass();
var url = "api/XYZ/PostMyService/updatedBy/" + loggedinuserid + "/myobjname/" + myobj;
HttpResponseMessage response = client1.PostAsJsonAsync(url, liTS).Result;
But the error/exception I am getting is:
HTTP Error 404.0 - Not Found
Most likely causes:
•The directory or file specified does not exist on the Web server.
•The URL contains a typographical error.
•A custom filter or module, such as URLScan, restricts access to the file.
Any idea how to resolve this? I am kind of hitting a wall on this.
Thanks in advance.
You have "api/XYZ/" prefixed in your client code, but I don't see it on your server code. I predict that you have it in your server configuration but if not you will see issues.
You can only have one object with the [FromBody] tag in WebAPI, it's not clear how your trying to pass the MyClass object.
Only simple objects, ints and strings, can be passed in the uri string so the MyClass object will not be transferred correctly.
I would recommend removing the MyClass object or creating a larger object that encapsulates your List<SomeType> andMyClass` and pass that in the body of the request with the [FromBody] decoration.
Here's more information on the topic
My application runs on jQuery and Spring MVC.
I have a grid which contains a hyperlink on reference number which is unique.
On clicking reference number, an ajax request is sent to the spring controller with reference number as the request parameter.
When an user clicks on two reference number quickly, two ajax requests are submitted to the server which results in the first request being aborted with status code 0 and second request is processed successfully.
The same happens for extreme slow network also.
When user clicks on two reference number after a certain interval, both the requests are processed successfully.
Can anyone please explain why the first request among the two gets aborted in slow network?
Below is the spring controller code
#RequestMapping(value = "/details/{ReferenceNo}", method=RequestMethod.GET)
public String getDetails(#PathVariable String ReferenceNo,ModelMap map) {
//server side stuffs
}
The client side code:
var $target = jQuery(e.target);
var href = application_path + $target.attr('href');
$.ajax({
url:href,
type:GET,
success:function(content){
//open the details
}
});
After checking this post, I know that Joomla use task=X.Y to call the controller to handle the request.
But if I click NEW button on com_categories component, it will access the URL of /administrator/index.php?option=com_categories&view=items and containing the POST data as below:
Then the URL get redirected to /administrator/index.php?option=com_categories&view=item&layout=edit.
My question is why the URL /administrator/index.php?option=com_categories&view=items don't have the task=X.Y and it can redirect to /administrator/index.php?option=com_categories&view=item&layout=edit?
I know that it contains the POST data with task=item.add, but which controller convert this POST data to the destination URL and get redirected to that?
Thanks.
Actually (on 2.5.14), when you click on the "New" button in the Category Manger view, the first request generates a POST:
POST /administrator/index.php?option=com_categories&view=categories HTTP/1.1
POST requests usually send query strings in the HTTP message body not just in the URL, in this case the POST request has the following form data in the body:
filter_search
filter_level
filter_published
filter_access
filter_language
limit 5
limitstart 0
order[] 1
order[] 1
order[] 1
order[] 1
order[] 2
batch[assetgroup_id]
batch[language_id]
batch[category_id]
batch[move_copy] m
extension com_content
task category.add
boxchecked 0
filter_order a.lft
filter_order_Dir asc
original_order_values 1,1,1,1,2
796194955f38a0d8db484c92d92ca5ce 1
You will notice this has a task parameter that has the value category.add (not item.add), this is taken into account by the JController class when getInstance($prefix, $config) is called in the com_categories entry point file:
$controller = JControllerLegacy::getInstance('Categories');
The JController class converts category.add into a $type of category and a $task of add. The $type value is used to assemble the path to the controller in conjunction with the components base path ( in this case /pathto/site/administrator/components/com_categories ).
So, when the instantiated JController class receives the ->execute($task) message in com_categories/categories.php entry point file:
$controller->execute(JRequest::getVar('task'));
it's actually already a controller of type CategoriesControllerCategory which is the what you would expect to handle the New button request.