1

Below is the request that I want to send. How do I do this via HttpClient? The endpoint is created in WebAPI.

Request

The endpoint code is something like this:

public string Post([FromBody]string sentence) {
    return sentence;
}

This is what I am trying:

public async static void getComponents(string inputString) {
        using (var client = new HttpClient()) {
            // New code:
            Console.WriteLine("sadsadsad");
            client.BaseAddress = new Uri("http://localhost:2500/");
            client.DefaultRequestHeaders.Accept.Clear();

            var abc = client.PostAsync("/api/image", new StringContent(inputString, "application/json")).Result;
            Console.WriteLine(abc.ToString());
        }
    }

The request is received on the server side, but the parameter is null. I get 415 Unsupported Media Type when I try adding application/json just to the DefaultRequestHeaders.

goluhaque
  • 561
  • 5
  • 17
  • I primarily want to know how to set the body data as this(without any parameter name or equal tos). – goluhaque Jun 26 '16 at 12:27
  • Possible duplicate of [POSTing JSON to URL via WebClient in C#](http://stackoverflow.com/questions/15091300/posting-json-to-url-via-webclient-in-c-sharp). Answer http://stackoverflow.com/a/24085601/3745022 – Eugene Podskal Jun 26 '16 at 12:31
  • @EugenePodskal I am trying what the selected answer on that questions says, but to no avail. – goluhaque Jun 26 '16 at 13:17
  • "to no avail" is not a proper problem statement - http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist – Eugene Podskal Jun 26 '16 at 13:19
  • Also, I have told you to use the second answer - http://stackoverflow.com/questions/15091300/posting-json-to-url-via-webclient-in-c-sharp. The first one uses vanilla WebRequest. – Eugene Podskal Jun 26 '16 at 13:23
  • @EugenePodskal sorry, I updated my question to HttpClient, not WebClient. – goluhaque Jun 26 '16 at 16:41

0 Answers0