cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
stifler10
New Member

Sending PDF file converted to base64 to an Endpoint via HTTP POST request

Hi Guys.

 

I am using Xamarin Forms and I have a requirement which I must send a PDF file that is converted to base64 string to an API Endpoint.

I'm having trouble sending it. I always receive this error. 

"HTTP 406 Not Acceptable. There was an error with your request. Please check your payload and then try again."

 

This is the Endpoint and the details of the Json body.

Endpoint

 

HTTP-POST:

https://goodmorning-axa-dev.azure-api.net/upload

 

HTTP-HEADERS:

x-axa-api-key: your-key

Content-Type: application/json

 

Request Body JSON

{

"file": {

"mime": "application/pdf",

"data": "base64-data="

}

}

 

Here is how I convert my PDF to base64

 

        private async Task ConvertPdf()
        {
            const string pdfFileName = "MyPDF.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));
                }
            }
        }

        private async void Submit()
        {          
            var file = new MyFile();
            var pdf = new PdfFile();
            file.MimeType = "application/pdf";
            file.Base64Data = "base64-data=" + pdfBase64;
            pdf.myFile = file;
            await _iregister.Send(pdf.myFile);
        }

 

 

This is my HTTP post request.

The myFile is my PDF that is already converted to base64 string.

 

            var apiKey = "######################";
            var jsonContent = JsonConvert.SerializeObject(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);
            }

 

Here is the format of the serialized JSON file. (I made it shorter just to show the format)

 

{
    "mime":"application/pdf",
    "data":"base64-data=JVBERi0xLjQKJe+/ve+/ve+/ve+/vQoxIDAgb2JqIAo8PAovU3VidHlwZSAvRm9ybQovVHlwZSAvWE9iamVjdAovTWF0cml4IFsxIDAgMCAxIDAgMF0KL0Zvcm1UeXBlIDEKL1Jlc291cmNlcyAKPDwKL1Byb2NTZXRzIFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXQowolJUVPRgo="
}

 

I am not sure if I am missing something in the Header or Body. Please help.

2 REPLIES 2
PrasadAthalye
Super User
Super User

@stifler10 - Can you add accept header?

 

Accept: application/json

 Like and Mark this as answer if it resolves your issue.

Please Like and Mark this as Answer if it resolves your Issue.

Power Automate Community
ginxx009
New Member

Hi was this solve? Because I am also having thesame problem and error code.

Helpful resources

Announcements
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

Users online (75,460)