Vinh Nguyen | Posts: 7

406 Error When Retrieving Attachment

0 votes

Hi, 

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


Approved Answer
Duo_Liang | Posts: 3776

Reply to: 406 Error When Retrieving Attachment

0 votes

Hi 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

 


Vinh Nguyen | Posts: 7

Reply to: 406 Error When Retrieving Attachment

0 votes

It works, thanks alot.


Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off