通过JSON API端点遇到状态码400

问题描述

我正在尝试将转换为Base64的PDF文件发送到API端点,但是遇到状态代码400,这是错误请求错误。首先,我将PDF转换为base64格式,然后通过使用Refit Post将其发送到API端点。我的PDF文件位于Visual Studio中我的Android项目中的Assets文件夹中。还请参见我的http-header,http-post和Request正文Json的屏幕截图。

这是我的 Requirements

课程

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; }
}

接口:

[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();
                }
            };

当我调用UploadPdf时,我总是会遇到状态码400错误的请求。我不确定实现Refit的方式是否有错误,或者我是否执行了将PDF转换为Base64的正确方法。需要帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)