I’m working on two projects, the first project(.net 5.0) need to request the bytes of a file to the second project(.net 8.0).
Method on First project:
public async Task<byte[]> GetTemplateAsync()
{
try
{
string uri = $"{_uri}GetTemplate";
var result = await _httpClient.GetAsync(uri);
if (!result.IsSuccessStatusCode)
{
throw new Exception(await result.Content.ReadAsStringAsync());
}
byte[] template = await result.Content.ReadAsByteArrayAsync();
return template;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
Endpoint on Second Project:
[HttpGet("/GetTemplate")]
[Produces("application/octet-stream")]
public HttpResponseMessage GetTemplate()
{
try
{
byte[] boletoTemplate = _templateService.GetTemplate();
HttpResponseMessage response = new(HttpStatusCode.OK)
{
Content = new ByteArrayContent(boletoTemplate)
};
return response;
}
catch (NotFoundException ex)
{
return new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(ex.Message)
};
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(ex.Message)
};
}
}
The result from _httpClient.GetAsync(uri)
on First project method is Not Acceptable
. This is produced when I put the [Produces("application/octet-stream")]
on the Second project’s endpoint, when I change to application/json
the First project result is Ok
but the file bytes are all different.