Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync

后端 未结 5 1565
灰色年华
灰色年华 2020-11-27 14:20

I want to send dynamic object like

new { x = 1, y = 2 };

as body of HTTP POST message. So I try to write

var client = new H         


        
相关标签:
5条回答
  • 2020-11-27 14:31

    I use this class:

    public class JsonContent : StringContent
    {
        public JsonContent(object obj) :
            base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
        { }
    }
    

    Sample of usage:

    new HttpClient().PostAsync("http://...", new JsonContent(new { x = 1, y = 2 }));
    
    0 讨论(0)
  • 2020-11-27 14:35

    Microsoft now recommends using an IHttpClientFactory with the following benefits:

    • Provides a central location for naming and configuring logical HttpClient instances. For example, a client named github could be registered and configured to access GitHub. A default client can be registered for general access.
    • Codifies the concept of outgoing middleware via delegating handlers in HttpClient. Provides extensions for Polly-based middleware to take advantage of delegating handlers in HttpClient.
    • Manages the pooling and lifetime of underlying HttpClientMessageHandler instances. Automatic management avoids common DNS (Domain Name System) problems that occur when manually managing HttpClient lifetimes.
    • Adds a configurable logging experience (via ILogger) for all requests sent through clients created by the factory.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1

    Setup:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpClient();
            // Remaining code deleted for brevity.
    

    POST example:

    public class BasicUsageModel : PageModel
    {
        private readonly IHttpClientFactory _clientFactory;
    
        public BasicUsageModel(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }
        
        public async Task CreateItemAsync(TodoItem todoItem)
        {
            var todoItemJson = new StringContent(
                JsonSerializer.Serialize(todoItem, _jsonSerializerOptions),
                Encoding.UTF8,
                "application/json");
                
            var httpClient = _clientFactory.CreateClient();
            
            using var httpResponse =
                await httpClient.PostAsync("/api/TodoItems", todoItemJson);
        
            httpResponse.EnsureSuccessStatusCode();
        }
    

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1#make-post-put-and-delete-requests

    0 讨论(0)
  • 2020-11-27 14:36

    I would add to the accepted answer that you would also want to add the Accept header to the httpClient:

    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
    0 讨论(0)
  • 2020-11-27 14:45

    You are right that this has long since been implemented in .NET Core.

    At the time of writing (September 2019), the project.json file of NuGet 3.x+ has been superseded by PackageReference (as explained at https://docs.microsoft.com/en-us/nuget/archive/project-json).

    To get access to the *Async methods of the HttpClient class, your .csproj file must be correctly configured.

    Open your .csproj file in a plain text editor, and make sure the first line is
    <Project Sdk="Microsoft.NET.Sdk.Web">
    (as pointed out at https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj#the-csproj-format).

    To get access to the *Async methods of the HttpClient class, you also need to have the correct package reference in your .csproj file, like so:

    <ItemGroup>
        <!-- ... -->
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- ... -->
    </ItemGroup>
    

    (See https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#adding-a-packagereference. Also: We recommend applications targeting ASP.NET Core 2.1 and later use the Microsoft.AspNetCore.App metapackage, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage)

    Methods such as PostAsJsonAsync, ReadAsAsync, PutAsJsonAsync and DeleteAsync should now work out of the box. (No using directive needed.)

    Update: The PackageReference tag is no longer needed in .NET Core 3.0.

    0 讨论(0)
  • 2020-11-27 14:46

    You should add reference to "Microsoft.AspNet.WebApi.Client" package (read this article for samples).

    Without any additional extension, you may use standard PostAsync method:

    client.PostAsync(uri, new StringContent(jsonInString, Encoding.UTF8, "application/json"));
    

    where jsonInString value you can get by calling JsonConvert.SerializeObject(<your object>);

    0 讨论(0)
提交回复
热议问题