The 'Content-Length' header must be modified using the appropriate property
Closed this issue · 13 comments
Hi,
I am new to github and trying to use RestShart.Portable for a xamarin cross platform mobile app. I am new to RestSharp.Portable and rest APIs in general. For a windows .net app, I have used below piece of code with RestSharp and it is working great. However as soon as used the same code in RestSharp.Portable with RestSharp.Portable.WebRequest, I am getting following inner exception:
"The 'Content-Length' header must be modified using the appropriate property or method.
Parameter name: name"
I have spent a lot time digging but not making any progress. I will really appreciate if can get any help.
here is my test code:
var request = new RestRequest ("MyResource", Method.POST);
string postData = myJsonToPost;
request.AddParameter("application/json; charset=utf-8", postData, ParameterType.RequestBody);
Task task;
try
{
task = restClient.Execute(request);
task.Wait();
IRestResponse response = task.Result;
}
catch (Exception ex)
{
}
The rest request object (when using the underlying HttpClient) need its ContentLength property be set instead of using the generic content headers API. This seems to be a bug. I'll take a look.
Thanks for your response, fubar-coder. If you know any quick work around, let me know. I will also try to debug source.
@calc1729 Do you use the HttpClient
or WebRequest
variant of the library?
I have been testing WebRequest variant, v4.0.7 to be precise.
Version 4.0.8 should be available soon.
Fubar-coder, many thanks for prompt fix!
Hi Mark, I got a chance to test the updated library but I am still getting the same exception. I am running the same code for webrequest. Just want to let you know in case you can still think of something. Thank.s
Can you please provide a sample project?
Hi Mark, please find the sample project here (it is more than 10mb):
https://drive.google.com/drive/folders/0B33wDjMVxKddOEE3My1kUGtDZjg?usp=sharing
I have tried to replicate the exact scenario. We have set up postgresql and postgrest API on an EC2 machine. In the project DataAccess, I am trying to receive output from a function call with some input parameters. I have already tested and this works for Restsharp. Let me know if you can figure the issue. Thanks
You just found out about some nasty limitation of the portable class libraries. The HttpWebRequest
that is available for PCLs doesn't support the ContentLength
property. The only workaround for this limitation is, that you add the FubarCoder.RestSharp.Portable.WebRequest
package only to your main projects (e.g. TestApp.Droid
) and only use FubarCoder.RestSharp.Portable.Core
elsewhere.
In other words: You may want to use dependency injection or factory classes to create your RestClient
instance from within your main platform specific project.
I attached the project (converted to VS2017, updated Xamarin packages):
EDIT: Using PostgreSQL hosted on AWS as test target is a bit overkill. You can use httpbin.org instead, which allows a wide range of requests to be tested.
EDIT2: Make sure that your service gets a new ID, as your old is probably already well known as a target for hackers.
Many thanks, Mark. Great to know all this. Yes, I will be replacing the machine soon. As I mentioned, new in the field and making my way through it. :)
I tested this in more detail. It seems GET works fine. However POST throws the exception "System.Net.WebException: The remote server returned an error: (500) Internal Server Error." I am testing it with the project you have created. However I am using the service URL that I provided in previous project. I can see the request being processed fine at the service logs. It seems the issues is with interpreting the response or with some header. Any input will be helpful. Thanks
The internal server error is usually the result of an exception thrown during the server side request processing. When something goes wrong in the client, you'll get a different exception.