I have some simple Python
import requests
response = requests.post(url+'users/', data = 'hallo')
And a simple ASP.Net Core API
public void Post(string value)
{
var newUser = JsonConvert.DeserializeObject<User>(value);
// [...]
}
When I breakpoint the C# code the incoming parameter 'value' is always null. I have tried sending JSON instead of a simple string, setting the headers to 'text/plain', 'application/json', or 'application/x-www-form-urlencoded' in Python but the result is the same. I have tried decorating the parameter [FromBody] in the ASP.Net Core API, but then I get a 400 error ("The input was invalid").
What am I missing?
(EDIT. Here is a hacky fix, definitely not an answer, but it may help people see what's wrong.
public void Post(string value)
{
Request.EnableRewind();
var body = "";
using (var reader = new StreamReader(Request.Body))
{
Request.Body.Seek(0, SeekOrigin.Begin);
body = reader.ReadToEnd();
}
value = body;
var newUser = JsonConvert.DeserializeObject<User>(value);
// [...]
}
Then value is set correctly. But if the value is sitting there in the body of the post, it is frustrating that using [FromBody] results in a 400 error.)
[FromBody]string valuedata = { 'value' : 'hallo' }