1

I have a simple DTO object that looks like the following:

public class InstructionComponents
    {
        public int ApplicationNumber { get; set; }
        public string FurtherComments { get; set; }
    }

I want to be able to send this object through a POST request to an API endpoint that uses ASP.NET MVC. However I want to make sure that the data is sent using the body of the request, and isn't just appended to the url like in a GET.

This is simple enough using get requests and can be achieved with the following code.

            var url = //endpoint url   
            using(var httpClient = new HttpClient())
            {
                var response = httpClient.GetStringAsync(url).Result;
                return result;
            }

I know that I can serialise the object to a JSON string using a library, but then what do I do with the string?

Jake12342134
  • 1,539
  • 1
  • 18
  • 45
  • Note that you should not be instantiating a new `HttpClient` instance for every request. See [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8), where it says, "HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors." – Heretic Monkey Apr 30 '19 at 13:32
  • Possible duplicate of [How do I pass an object to HttpClient.PostAsync and serialize as a JSON body?](https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body) – Heretic Monkey Apr 30 '19 at 13:37

2 Answers2

0

This is an POST example maybe can useful:

 var values = new Dictionary<string, string>
    {
       { "thing1", "hello" },
       { "thing2", "world" }
    };

    var content = new FormUrlEncodedContent(values);

    var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

    var responseString = await response.Content.ReadAsStringAsync();
hassan.ef
  • 1,300
  • 2
  • 11
  • 19
  • I've seen this, but is there any way to encode a custom object instead of a dictionary? – Jake12342134 Apr 30 '19 at 13:37
  • use this link: https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body – hassan.ef Apr 30 '19 at 13:40
0

The easiest way is to the extension method PostAsJsonAsync()

This method will handle any serialization of the object that is necessary.

From the documentation it can be found

Namespace: System.Net.Http Assembly: System.Net.Http.Formatting (in System.Net.Http.Formatting.dll)

Contrived example from the web:

static async Task<Uri> CreateProductAsync(Product product)
{
    HttpResponseMessage response = await client.PostAsJsonAsync(
        "api/products", product);
    response.EnsureSuccessStatusCode();

    // return URI of the created resource.
    return response.Headers.Location;
}
Fran
  • 6,440
  • 1
  • 23
  • 35