0

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?

pg_hansen
  • 11
  • 2
  • Have you tried setting the `ContentLength`? You need to do that before writing to the screen, as in the docs - _"If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream."_ – stuartd Jun 14 '18 at 17:11
  • Async void is bad in my eyes. IActionResult post expects items in body whereas async void is string from url. I would use fiddler to track the request from outside postman to see the header and body info as well. –  Jun 14 '18 at 17:37
  • I hadn't known about Fiddler. After installing and configuring it I learned this interesting fact: Fiddler reports that the content type is text/html; charset=us-ascii and the length is 334 (which is correct for my json string). Why would this be? I explicitly set the content type to application/json. – pg_hansen Jun 14 '18 at 18:24
  • Here's the JSON object (shown in text view from Fiddler): {"Firstname":"Clem","Lastname":"Worker","City":"Billville","State":"CA","Country":"USA","UserName":"UhClem@FST.com","Password":"CloseBClothesMode","EntityId":0,"Created":false} – pg_hansen Jun 14 '18 at 18:38
  • Here's the JSON from the POSTMAN post as captured by Fiddler: "{\"Firstname\":\"Clem\",\"Lastname\":\"Worker\",\"City\":\"Billville\",\"State\":\"CA\",\"Country\":\"USA\",\"UserName\":\"UhClem@FST.com\",\"Password\":\"CloseBClothesMode\",\"EntityId\":0,\"Created\":false}" – pg_hansen Jun 14 '18 at 18:44
  • Okay, learned something new. I decided to try the JSON string from my client in POSTMAN and, low and behold, I got a null string! Now I just have to find out why JsonConvert.SerializeObject() is giving me the string that doesn't work... – pg_hansen Jun 14 '18 at 19:20
  • Back to square one. I now have the client producing the correct JSON string (as verified by using it with POSTMAN), but I stlll get a null string. The frustration is strong with this one... Looking again at Fiddler, I see the JSON string stripped of its backslashes which seems to be the problem. – pg_hansen Jun 14 '18 at 19:49

1 Answers1

0

I found the answer here: ASP.NET Core API POST parameter is always null

I must admit that I hadn't considered this approach when I first ran across it earlier in the day, but to paraphrase Sherlock Holmes, once you have exhausted the other possibilities...

In short, I changed the Content type to text/plain and added a formatter to my web api Startup.cs file (as shown in the other link) and this worked.

pg_hansen
  • 11
  • 2