Send Http Post Message in ASP.NET Core Using Httpclient Postasjsonasync

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

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>);

Response 400 on the post of httpclient in dotnet core

The correct way to post file and additional data by HttpClient like below:

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44331/api/");
var formContent = new MultipartFormDataContent();
formContent.Add(new StringContent(agentCreateDto.Id.ToString()), nameof(agentCreateDto.Id));
formContent.Add(new StringContent(agentCreateDto.Message), nameof(agentCreateDto.Message));
formContent.Add(new StringContent(agentCreateDto.UserType), nameof(agentCreateDto.UserType));
//other proerties....
formContent.Add(new StreamContent(agentCreateDto.ProfileImage.OpenReadStream()), nameof(agentCreateDto.ProfileImage), Path.GetFileName(agentCreateDto.ProfileImage.FileName));

//change PostAsJsonAsync to PostAsync.....
var responseTask = client.PostAsync("/Agent/CreateAgent", formContent);
responseTask.Wait();

var result = responseTask.Result;
//more code...
}

POSTing JsonObject With HttpClient From Web API

With the new version of HttpClient and without the WebApi package it would be:

var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
var result = client.PostAsync(url, content).Result;

Or if you want it async:

var result = await client.PostAsync(url, content);

How can i post both file and text data to web api using asp net core http client?

How can i post both file and text data to web api using asp net core http client

public async Task<IActionResult> AddLayout([FromForm] Layout1 layout)

If you'd like to make HTTP request(s) with form data to your AddLayout endpoint, please refer to the following code snippet.

//...
var formContent = new MultipartFormDataContent();

formContent.Add(new StringContent("blabla"), "Titulo");
//...

formContent.Add(new StringContent(Path.GetFileName(FotoN1.FileName)), "FotoN1");

formContent.Add(new StreamContent(FotoN1.OpenReadStream()), "FotoN1File", Path.GetFileName(FotoN1.FileName));

//...
//for other properties, such as FotoN2, FotoN2File etc
//...

var response = await _client.PostAsync(_APIserver + "/api/cliente/AddLayout", formContent);

if (response.IsSuccessStatusCode)
{
//...

HttpClient and form parameter names

I posted this to https://github.com/dotnet/runtime/issues/72447

Either form is correct, apparently

ASP.NET Core convert http response message to IEnumerable

You need to await the content from the HttpResponseMessage as string.

var responseContent = await httpResponseMessage.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<MyClass>(responseContent);

StatusCode: 400 BadRequest using HttpClient for POST and uploading a PDF

Check the last EDIT, I fixed it myself like that. Commenting this so I can mark the question as solved



Related Topics



Leave a reply



Submit