Отправить список объектов с помощью HttpClient в C#C#

Место общения программистов C#
Ответить
Гость
 Отправить список объектов с помощью HttpClient в C#

Сообщение Гость »


Я общаюсь с конечной точкой, созданной другой командой в ядре asp.net, которая принимает ([FromBody] кандидат-кандидат)
Объект кандидата выглядит следующим образом:
< бр />

Код: Выделить всё

public class Applicant
{
public string FirstName{get; set;}
public string LastName{get; set;}

public List Reports{get; set;}
}
and the list of reports is from this object:

Код: Выделить всё

public class Reports
{
public string ReportName {get;set;}
public byte[] ReportFile {get;set;}

}
I am trying to call the endpoint twice where a send an applicant with one reportfile
and a second time where I send an applicant with two report files. This is what I have so far:

Код: Выделить всё

byte[] report1 = new UTF8Encoding(true).GetBytes("This is a test report1");
byte[] report2 = new UTF8Encoding(true).GetBytes("This is a test report2");
byte[] report3 = new UTF8Encoding(true).GetBytes("This is a test report3");

var reportOjbect = new
{
FirstName = "John",
LastName = "Doe",
Reports = new []
{
new {
ReportName = "6 week Report",
ReportFile = report1
}

}

};

var baseUrl = "localhost:1111";
var endpoint = "/api/validate-reports";

HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(baseUrl);

var content = new MultipartFormDataContent();
content.Add(new StringContent(reportOjbect.FirstName), "FirstName");
content.Add(new StringContent(reportOjbect.LastName), "LastName");
//How to add list of reports here that contain both the report name and the report file

var request = new HttpRequestMessage(HttpMethod.Post, $"{baseUrl}{endpoint}")
{ Content = content};

var response = await httpClient.SendAsync(request);

if (!response.IsSuccessStatusCode)
{
throw new Exception($"Failed to send email: {response.StatusCode}");
}
return Results.Ok("Reports were processed");
How do I add a list of reports to the content object.


Источник: https://stackoverflow.com/questions/781 ... in-c-sharp
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»