如何解决通过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 举报,一经查实,本站将立刻删除。