cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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.

1 REPLY 1
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

Helpful resources

Announcements
Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

New Super Users

Meet the Power Automate Super Users!

Many congratulations to the Season 1 2021 Flownaut Crew!

New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Users online (72,603)