PainlessHttp icon indicating copy to clipboard operation
PainlessHttp copied to clipboard

POST with no data doesn't work

Open DanielSundberg opened this issue 8 years ago • 2 comments

I have the following API-method:

[Route("api/xyz")]
[HttpPost]
public IHttpActionResult PostAPIXYZ() {
    return Ok("ok");
}

Then calling it with:

using PainlessHttp.Http;

var config = new Configuration {
     BaseUrl = "http://localhost:81",
};

HttpClient = new HttpClient(config);    
var result = HttpClient.PostAsync<MyResult>("api/xyz", null).Result;

Result: Http status: LengthRequired (411)

In powershell it works ok:

> (Invoke-WebRequest http://localhost:81/api/xyz -Method POST).StatusDescription
OK

Using Http client from System.Net.Http works ok:

using System.Net.Http;

HttpClient HttpClient = new HttpClient {
    BaseAddress = new Uri(http://localhost:81), 
};
var result = HttpClient.PostAsync("api/xyz", new StringContent(""))

DanielSundberg avatar Dec 02 '16 12:12 DanielSundberg

Hi @DanielSundberg, Thanks for reporting this. PainlessHttp has not been updated for some time, one reason is that it's based on HttpRequester, which is discontinued in dotnet core.

Anyways, I believe that the problem is that you're in fact sending a POST body, the (empty) StringContent. It will eventually get serialized. You could try string.Empty or try the method PerformRaw that does not require you to specify any body.

Hope this helps!

pardahlman avatar Dec 02 '16 17:12 pardahlman

string.Empty doesn't work.

HttpClient.PerformRawAsync(HttpMethod.Post, "api/xyz") with no data still gives me Http status LengthRequired.

Using System.Net.Http.HttpClient for now.

DanielSundberg avatar Dec 05 '16 07:12 DanielSundberg