0

I know there are similar posts regarding calling an HttpPost method but nothing I've read/implemented has worked for me. I'm simply trying to do a POST call but the response is always null for some reason. I'm new to web development and ASP.

Here's my WebClient code:

using (WebClient webClient = new WebClient())
        {
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            var dataToSend = "=testingu";
            var response = webClient.UploadString("http://localhost:5000/core/test", "POST", dataToSend);
            Console.WriteLine("Response is: " + response);
        }

and here's my HttpPost code:

    [HttpPost("/core/test")]
    public string PostObj([FromBody]dynamic input)
    {
        string result = "";
        if (input == null)
            System.Console.WriteLine("Input is null.");
        else
            System.Console.WriteLine("Input is not null: " + input);

        return result;
    }

Whenever PostObj is called, the "Input is null" line is always executed when I'm expecting to see "Input is not null: testingu" printed. It seems like my WebClient code is sound, but I'm pretty new to this so any help is appreciated.

Roka545
  • 3,404
  • 20
  • 62
  • 106
  • Also it might be a possible duplicate: http://stackoverflow.com/a/20370629/1260204 – Igor Apr 06 '16 at 21:21
  • I think you need to use the HttpWebRequest/Response, see this: http://stackoverflow.com/questions/3892042/create-http-post-request-and-receive-response-using-c-sharp-console-application – nivs1978 Apr 06 '16 at 21:30
  • I've found out that changing the PostObj method parameter to (string input) instead of ([FromBody]dynamic input) allows the input to come through as intended, but I'm not supposed to change the PostObj code (it's part of an existing codebase beyond my editing rights). – Roka545 Apr 07 '16 at 17:28

0 Answers0