I am using this code to send a list of file paths to a api and receive response from api.
But it is not working. I do not have access to the file. The file is only available to the api. A user like me cannot open the file directly, so this line fails. The api has access to the file, processes it and sends me the response. How should I modify this code to take only filepath and not the byte[] arrray content
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePathList[i]));
Code
using (var form = new MultipartFormDataContent())
{
// Load the file and add it to the form data
for (int i = 0; i < filePathList.Count; i++)
{
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePathList[i]));
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
form.Add(fileContent, "file", "one.pdf");
}
// Set headers
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
// Post the form data to the API
HttpResponseMessage response = await httpClient.PostAsync(url, form);
Console.WriteLine($"Response received with status code: {response.StatusCode}");
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Request successful. Reading response content...");
// Read and return the response body
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response content read successfully.");
return responseBody;
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error content: {errorContent}");
throw new HttpRequestException($"Request failed with status code: {response.StatusCode}, content: {errorContent}");
}
1