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

后端 未结 5 1590
灰色年华
灰色年华 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: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();

提交回复
热议问题