0
On my system, I need to send a received form file to an API that will be saved in Azure. For this submission I need to pass a Bearer Token, the part of the token I was able to do and receive, testing by Postman I can send and receive the url that was generated when uploading the file. In my project . Net, I cannot send the file and receive its url to write to the database. I need to pass this token I generated in another class and upload the file using the API URL. I’ll put in the code I’ve written so far.
[HttpPost]
public async Task<IActionResult> UploadAzure([FromForm] IFormFile file)
{
var Token = await _tokenAzure.ReturnToken();
var _urlApi = "https://mysite.dev/api/file/savepdf";
var _tokenApi = Token.AccessToken;
if (file == null || file.Length == 0)
return Content("file not selected");
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
// Pass the handler to httpclient(from you are calling api)
HttpClient client = new HttpClient(clientHandler);
byte[] data;
using (var br = new BinaryReader(file.OpenReadStream()))
data = br.ReadBytes((int)file.OpenReadStream().Length);
ByteArrayContent bytes = new(data);
MultipartFormDataContent multiContent = new()
{
{ bytes, "file", file.FileName }
};
var result = client.PostAsync(_urlApi, multiContent).Result;
return RedirectToAction("Index");
}
Missing pass token on request.
– Jéf Bueno
And how should I pass the Token to API?
– João
The way it does in Postman, like a request header.
– Jéf Bueno