i'm trying to maintain a project which is trying to post images (yes multiple) from mobile devices to a dot-net web api.
The default content between api and mobile devices is application/json.
public async Task<HttpResponseMessage> PostExpertiseForm(List<string> images){}
This is the actual method for posting and in this method they are trying to write base64 strings (images) to files.
But the problem is when i tried to call this api, for the first time web api binds images with my base64 strings, but all next requests (same request with the first one) are binds to null.
I did some google search and tought about it was a request size problem and i added this lines to Web.config :
<httpRuntime targetFramework="4.6" maxRequestLength="2147483647" />
<requestLimits maxAllowedContentLength="2147483648" />
<jsonSerialization maxJsonLength="2147483644">
Didnt work.
What is the real problem here? Why my first request worked?
Thanks for your advice!
The problem was not about json size. There was a mechanism for logging every response and request, in log request method i saw this line var request=await Request.ReadAsStringAsync();
After little searching about parameterless post methods in web api i found that question why-is-the-body-of-web-api-request-read-once
And i realised that i was try to read the request body twice and caused dead lock.
I changed the logic of logging. The problem has gone.
Related
This is a style question about REST API design
I have an API which returns a particular resource, and can return either the fields of the resource as a JSON object, or a PDF representation of the resource. The normal REST way of doing that is to use the same URL, but return either the JSON object or the PDF data depending on the "Accept" header of the request.
This has been fine when calling the API from a client application. But now I am writing a web application, and I want to display the PDF. I can fetch the PDF data with XMLHttpRequest, but there isn't an easy way to display it. (I recall some hack involving passing the whole base64-encoded content in the URL, but that is both flaky and disgusting).
The easy way to display a PDF in a web application is window.open(), but I can't pass an Accept header to that (there are a few questions here for people asking how to do that).
This seems like a potentially common situation. What's the best workaround? Stick ?pdf or /pdf or ?accept=pdf onto the url? Is there a de facto standard? Or is there a solution I haven't thought of (maybe treating "application/pdf" as the default request mime type, and only returning the JSON object if the Accept header is "application/json")?
Actually the more I think of it, the more that last idea (return PDF unless explicitly asked for application/json in the Accept header) seems like the right answer. A web browser can display PDFs (at least usually, these days) and not JSON objects, so a request made natively by a browser should always get the PDF, and REST clients that wants JSON data would normally explicitly ask for application/json, so the API server should not do that by default if it has a better browser-compatible form.
Since it took actually explaining this whole problem into the question box to make me think of the answer I posted the question anyway.
I'm using Django REST Framework and all the API calls come from Android and iOS apps. The system works perfectly most of the time, however, when an internal server error happens and I get an email from Django, the POST of the WSGIRequest contains <could not parse> instead of the actual posted JSON data (even though 'CONTENT_TYPE': 'application/json' is also in the header, and the data is sent as JSON).
This is really annoying, as it would be great to see the request body that actually causes the error, not just the stacktrace.
The <could not parse> part is very similar to this question (in the ModPythonRequest part): django request.POST contains <could not parse>, except the actual problem is slightly different. Also the reference link in that question (https://stackoverflow.com/questions/12471661/mod-python-could-not-parse-the-django-post-request-for-blackberry-and-some-andro) seems to have gone down, even though the name looked very promising.
I'm on Django 1.6.2 and DRF 2.3.13.
The POST dictionary of the WSGIRequest is always going to be invalid, because it is intended to hold the parsed form data when the Content-Type is application/x-www-form-urlencoded or multipart/form-data.
The data you want is in the body attribute of the WSGIRequest object, which isn't output when that object is converted to a string to be written to the log.
When using Django REST framework, you will typically want to access request.DATA (which will handle whatever formats you have parsers configured for - defaulting to form content and JSON) instead of Django's standard request.POST, which will only handle form encoded data.
I have a webservice method that if I call directly via url GET returns XML without issue.
However, POST to that same url with Content-Type Json, it fails.
I think I can figure out the issue (I'm guessing it's an encoding or bad character somewhere in there) but I don't know how to debug the problem.
If I set a breakpoint in the webservice, it runs to completion. The failure appears to be happening AFTER the method returns, but BEFORE the json is returned to the caller.
How can I get in between to trace the error?
Please let me know if I can provide more context to help, but I really just need to know how to get in there.
EDIT:
The web service is configured to receive POST and return JSON and in fact DOES correctly return JSON in some cases. However, there are certain calls that are failing, so I need a way to trace this or debug it somehow and figure out why some calls are not working.
The web service is likely not configured to receive POST requests, especially if you are receiving a 405 Method Not Allowed response status.
Although I didn't find a way to debug or intercept the request to find the exact answer, it turns out the problem was the size of the content being returned by the webservice. Following this answer: ASP.NET WebMethod with jQuery json, is there a size limit?
and increasing the json limit fixed the issue!
Is there a way I could have trapped this to find the error without just guessing it was a size limit?
I am trying to understand how POST and/or GET methods work in terms of the actual browser.
I am attempting to contact an API which requires API key, method you wish to use on their side, and an IP address at the minimum.
My original idea was to do something like this:
I feel like I'm on the right track, it does something and gets an error as opposed to telling me the page does not exist. I'm expecting either JSON or XML in response as the API supports both but instead I get this error:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
Upon studying the documentation of the API more, I found something saying that methods are called using HTML form application/x-www-form-urlencoded and the resuource models are given as form elements.
I tried researching what that means to see what the problem was and found this site http://www.w3.org/TR/xforms11/ but I'm still unclear.
Ideas?
It seems to mean that the application is expecting a POST method but you're doing a request with a GET method (when you use the querystrings).
Since you can't just do browser requests using POST using the address bar, you may need to:
Construct a simple JS function that does a xmlhttprequest request using that method instead, and running it from the console;
Create a simple HTML page that automates the above process, allowing you do make POST calls;
Using CURL instead, which is a great tool for testing those kinds of requests.
Hy,
I'm working on a jsFiddle with a openlayers example in it.
http://dev.openlayers.org/releases/OpenLayers-2.11/examples/snapping.html
At the moment it's not working because it's not getting a response for the http request to get data. How do I fix that?
The jsFiddle is here: http://jsfiddle.net/TcuxA/6/
Go to the line "// create three vector layers" in the script.
There are 3 requests for data. If you type the URLs in your browser you get the JSON, but my firebug gives 3 errors when I run the jsFiddle.
I tried fixing with jsFiddle echo ( http://doc.jsfiddle.net/use/echo.html ), but that didn't work. I don't know how to change the script to load the data otherwise.
Why can't I get the json by these URLs? What are good sollutions?
What you are experiencing is an exception being thrown by the XMLHttpRequest object, because you are using AJAX to call elements from different domain. This is better said, for example, in here:
"The XMLHttpRequest object is prevented from calling web services from outside its own domain. This is sensible given that if you called a script in one place and it, in turn, called a script on another server, it could leave an application open to all sorts of malicious scripts, hacks and exploits."
So the easiest way to do is to code it locally and call a local copy of the files (poly.json, line.json and point.json) that would reside on your local server. For testing if all displays on a map you could hard-code the files into your code. I am not sure how it could be achieved otherwise.
A good solution can be this : using github responses . You can store your example in github, along with the predefined XHR requests responses.