base64 encoded file upload via json api endpoint encountering Status code 400

1.6k views Asked by At

I'm trying to send a PDF file that is converted to Base64 to an API endpoint but I'm encountering a Status Code 400 which is Bad Request error. First, I'm converting my PDF to base64 format then send it to an API endpoint by using Refit Post. My PDF file is located in the Assets folder in my Android Project in Visual Studio. Please also see my screenshot for the http-header, http-post and Request body Json.

Here are my Requirements

Classes

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

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

Interface:

[Headers("Content-Type: application/json")]
interface IMyApi
{
    [Post("/upload")]
    Task UploadPdf([Body] PdfFile pdfFile, [Header("x-axa-api-key: apikey")] string apiKey);
}

MainActivity

myAPI = RestService.For<IMyApi>("https://goodmorning-axa-dev.azure-api.net");
string pdfBase64;

sendButton.Click += async delegate
            {
                Android.Support.V7.App.AlertDialog dialog = new EDMTDialogBuilder()
                .SetContext(this)
                .SetMessage("Sending...")
                .Build();

                try
                {
                    if (!dialog.IsShowing)
                        dialog.Show();

                    const string pdfFileName = "TEST.pdf";

                    using (var stream = await FileSystem.OpenAppPackageFileAsync(pdfFileName))
                    {                   
                        using (var reader = new StreamReader(stream))
                        {
                            var fileContents = await reader.ReadToEndAsync();    
                            pdfBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContents));
                        }
                    }

                    var pdf = new PdfFile();
                    var file = new MyFile();
                    

                    file.MimeType = "application/pdf";
                    file.Base64Data = "base64-data=" + pdfBase64;

                    pdf.file = file;

                    await myAPI.UploadPdf(pdf, "apiKey");

                    Toast.MakeText(this, "Registration Successful", ToastLength.Short).Show();

                    if (dialog.IsShowing)
                        dialog.Dismiss();    
                }
                catch (System.Exception ex)
                {
                    Toast.MakeText(this, ex.Message, ToastLength.Short).Show();
                    Console.WriteLine(ex.Message);

                    if (dialog.IsShowing)
                        dialog.Dismiss();
                }
            };

When I call the UploadPdf, I always encounter the statuscode 400 Bad Request. I'm not sure if I have error in the way I implement Refit or if I did the proper way of converting my PDF to Base64. Need help.

0

There are 0 answers