C# 通过json api端点上传base64编码的文件,遇到状态代码400

C# 通过json api端点上传base64编码的文件,遇到状态代码400,c#,xamarin,base64,http-status-code-400,refit,C#,Xamarin,Base64,Http Status Code 400,Refit,我试图将转换为Base64的PDF文件发送到API端点,但遇到状态代码400,这是错误的请求错误。首先,我将PDF转换为base64格式,然后使用Refit Post将其发送到API端点。我的PDF文件位于Visual Studio中我的Android项目的资产文件夹中。还请参见我的http头、http post和请求体Json截图 这是我的 班级 public class PdfFile { [JsonProperty("file")] public My

我试图将转换为Base64的PDF文件发送到API端点,但遇到状态代码400,这是错误的请求错误。首先,我将PDF转换为base64格式,然后使用Refit Post将其发送到API端点。我的PDF文件位于Visual Studio中我的Android项目的资产文件夹中。还请参见我的http头、http post和请求体Json截图

这是我的

班级

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);
}
主要活动

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();
                }
            };
myAPI=RestService.For(“https://goodmorning-axa-dev.azure-api.net");
字符串pdfBase64;
sendButton。单击+=异步委托
{
Android.Support.V7.App.AlertDialog对话框=新建EDMTDialogBuilder()
.SetContext(本)
.SetMessage(“正在发送…”)
.Build();
尝试
{
如果(!dialog.IsShowing)
dialog.Show();
常量字符串pdfFileName=“TEST.pdf”;
使用(var stream=await FileSystem.OpenAppPackageFileAsync(pdfFileName))
{                   
使用(变量读取器=新的流读取器(流))
{
var fileContents=wait reader.ReadToEndAsync();
pdfBase64=Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContents));
}
}
var pdf=新的PdfFile();
var file=new MyFile();
file.MimeType=“application/pdf”;
file.Base64Data=“base64 data=“+pdfBase64;
pdf.file=文件;
等待myAPI.UploadPdf(pdf,“apiKey”);
Toast.MakeText(这是“注册成功”,ToastLength.Short).Show();
if(dialog.IsShowing)
dialog.dismise();
}
catch(System.Exception-ex)
{
Toast.MakeText(这个,例如Message,ToastLength.Short).Show();
控制台写入线(例如消息);
if(dialog.IsShowing)
dialog.dismise();
}
};

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

您可能应该从示例中删除API密钥。URL中的“/upload”与示例一样在哪里?@jdweng/upload在IMyApi接口中[Post(“/upload”)]使用嗅探器并验证请求是否与需求相同。验证TLS版本是否为1.2/1.0。还要验证内容长度不是零。@jdweng我以前从未使用过嗅探器。你推荐什么样的嗅探软件?我试试看。