"HTTP 406 Not Acceptable" When trying to send PDF converted to base64. C# xamarin

598 views Asked by At

I'm doing a POST method to an end point with a PDF that is converted to base64 but I'm receiving a HTTP 406 CODE. I included in the body the application/pdf for mime type and I included the necessary header to accept my POST request but still I'm receiving the 406 code. Does the json body has any connection on receiving the 406 code or am I having a problems on my header? Please see attached image for the details of the end point and json body.

public class MyFile
{
    [JsonProperty("mime")]
    public string MimeType { get; set; }
    [JsonProperty("data")]
    public string Base64Data { get; set; }
}

public class PdfFile
{
    [JsonProperty("file")]
    public MyFile myFile { get; set; }
}

string pdfBase64;
const string pdfFileName = "file.pdf";
    
                    using (var pdfStream = await FileSystem.OpenAppPackageFileAsync(pdfFileName))
                    {
                        using (var pdfReader = new StreamReader(pdfStream))
                        {
                            var fileContents = await pdfReader.ReadToEndAsync();
                            pdfBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContents));
                        }
                    }
    
                    var file = new MyFile();
                    var pdf = new PdfFile();
                    file.MimeType = "application/pdf";
                    file.Base64Data = "base64-data=" + pdfBase64;
                    pdf.myFile = file;
    
    
                    var jsonContent = JsonConvert.SerializeObject(pdf.myFile);
                    string baseUrl = "https://goodmorning-axa-dev.azure-api.net/upload";
    
                    HttpClient httpClient = new HttpClient();
    
                    httpClient.DefaultRequestHeaders
                        .Accept
                        .Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
                    HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, baseUrl);
                    requestMessage.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                    requestMessage.Headers.Add("x-axa-api-key", apiKey);
    
                    HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
                    string responseAsString = await responseMessage.Content.ReadAsStringAsync();
    
                    if (responseAsString != null)
                    {
                        Console.WriteLine(responseAsString);
                    }
0

There are 0 answers