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.

3 REPLIES 3
PrasadAthalye
Community Champion
Community Champion

@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.

errollocampo
New Member

I know it is quite late for me to answer this but I have the exact problem encountered for my interview exam, just replaced 

 file.Base64Data = "base64-data=" + pdfBase64;

to file.Base64Data = pdfBase64; 

got HTTP 200 response:

{"Message":"HTTP 202 Accepted. Welcome Back! Resume uploaded on our system. Would you be available for a technical interview? Please try and schedule a time in our final endpoint can be found at https://xxxxxxxxxx","Result":202}

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Users online (1,771)