PainlessHttp
PainlessHttp copied to clipboard
POST with no data doesn't work
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(""))
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!
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.