406 Error When Retrieving Attachment
Thursday, March 14, 2024 at 03:42pmHi,
I have a method to Retrieve the attachment:
public async Task<byte[]> GetAttachmentContentAsync(string packageId, string attachmentId)
{
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.ParseAdd("application/pdf");
// Validate the request parameter against a known fixed string or valid source of IP address
if (!Uri.TryCreate($"packages/{packageId}/attachment/{attachmentId}", UriKind.Relative, out Uri uri))
throw new ArgumentException("Invalid URI");
var response = await _httpClient.GetAsync(uri);
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.ParseAdd("application/json");
if (!response.IsSuccessStatusCode)
throw new CriticalException($"Remote service returned error code {response.StatusCode}" +
$" with content {await response.Content.ReadAsStringAsync()}", response);
return await response.Content.ReadAsByteArrayAsync();
}
When making calls to:
packages/pS0-1Z7B6infg5ozG2TDtQXW73A=/attachment/BFhLSQsSVekV
I got 604 Not Acceptable.
However, getting other docs works just find (the code is remarkably similar as above)
packages/pS0-1Z7B6infg5ozG2TDtQXW73A=/evidence/summary
packages/pS0-1Z7B6infg5ozG2TDtQXW73A=/documents/bbbff404-2fee-4f2a-8258-ba871efe83a0/pdf
packages/pS0-1Z7B6infg5ozG2TDtQXW73A=/documents/default-consent/pdf
Reply to: 406 Error When Retrieving Attachment
Thursday, March 14, 2024 at 04:19pmHi Vinh Nguyen,
Thanks for your post! In short, can you try to use either "*/*" or "application/octet-stream" as accept header? Below is a quick example that works for me:
string url = $"{API_URL}/packages/8L5qG0I5PU0pKct5s6zX8IEv-uY=/attachment/jaIW5qJGrfQT";
HttpClient myClient = new HttpClient();
myClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", API_KEY);
myClient.DefaultRequestHeaders.Add("Accept", "*/*");
var attResponse = myClient.GetAsync(new Uri(url)).Result;
ByteArrayContent content = new ByteArrayContent(attResponse.Content.ReadAsByteArrayAsync().Result);
File.WriteAllBytes("..\\driver license.png", content.ReadAsByteArrayAsync().Result);
Debug.WriteLine("Attachment Downloaded");
Duo
Reply to: 406 Error When Retrieving Attachment
Friday, March 15, 2024 at 10:12amIt works, thanks alot.