This has me really stumped. I have a Web API handler for POST that looks like this:
[HttpPost]
public IActionResult Post([FromBody]string jsonString)
{
if (jsonString == null)
return new ObjectResult("Bad JSON");
My caller looks like this (borrowed from How to post JSON to the server?):
public async void PostJson(string path, string payload)
{
string url = string.Format("{0}{1}", BaseAddress, path); // Not the issue, the URL is good...
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(payload);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(string.Format("Response: {0}", result.ToString()));
Console.WriteLine("Press any key...");
Console.ReadKey();
}
} Here's the problem. When I use POSTMAN to post the JSON string (as supplied in payload), my WebApi gets the correct JSON string. Yet, when I use the code listed above (and many other methods I've tried by researching this) I consistently get a NULL value for jsonString in my web api.
Can anyone shed any light on this?