C#REST API文件从客户端代码上传获取400不良请求

发布于 2025-01-26 12:26:44 字数 1497 浏览 1 评论 0原文

我有一个以.NET核心写的REST API,该API接受文件作为多部分/表单数据的输入。当我从Swagger/Postman运行它时,API非常好。

这是API端点。

  [HttpPost("CreateStudy")]
  public ActionResult CreateStudy([FromForm] APIRequest request)
  {
     // rest of the code

这也是ApireQuest对象。它只有一个属性是Iformfile类型。

public class APIRequest
{             
    public IFormFile XMLFile { get; set; }
}  

到目前为止,它运行良好。问题是我正在尝试编写一个客户端代码,该代码将调用此API并从C#代码传递文件。

但是我总是在客户端代码中收到400 bad请求。

这是我尝试使用的客户端代码。

    public string CallServiceWithFileAsync(string EndPointURL, string FilePath)
    {
            string ResultStatusCode;            
        
            Uri uri = new Uri(EndPointURL);

            var Client = new HttpClient();
            Client.DefaultRequestHeaders.Clear();

            //Prepare Message
            HttpRequestMessage Message = new HttpRequestMessage();
            Message.Method = HttpMethod.Post;

            Message.Headers.Add("Accept", "application/octet-stream");
            Message.RequestUri = new Uri(EndPointURL);               

            using (Stream fileStream = File.OpenRead(FilePath))
            {
                var content = new StreamContent(fileStream);
                var response = Client.PostAsync(uri, content);
                ResultStatusCode = response.Result.StatusCode.ToString();
            }

            return ResultStatusCode;
    }

我在这里做错了什么?将文件发送到REST端点的正确方法是什么?

I have a Rest API written in .Net core that accepts a File as input as Multipart/Form-data. The API works absolutely fine when I run it from Swagger/Postman.

Here is the API endpoint.

  [HttpPost("CreateStudy")]
  public ActionResult CreateStudy([FromForm] APIRequest request)
  {
     // rest of the code

Also here is the APIRequest object. it has only one property which is IFormFile Type.

public class APIRequest
{             
    public IFormFile XMLFile { get; set; }
}  

So far it works well. The problem is that I am trying to write a client side code that will call this API and pass the File from C# code.

But I am always getting a 400-Bad request in the client code.

This is the client code I am trying with.

    public string CallServiceWithFileAsync(string EndPointURL, string FilePath)
    {
            string ResultStatusCode;            
        
            Uri uri = new Uri(EndPointURL);

            var Client = new HttpClient();
            Client.DefaultRequestHeaders.Clear();

            //Prepare Message
            HttpRequestMessage Message = new HttpRequestMessage();
            Message.Method = HttpMethod.Post;

            Message.Headers.Add("Accept", "application/octet-stream");
            Message.RequestUri = new Uri(EndPointURL);               

            using (Stream fileStream = File.OpenRead(FilePath))
            {
                var content = new StreamContent(fileStream);
                var response = Client.PostAsync(uri, content);
                ResultStatusCode = response.Result.StatusCode.ToString();
            }

            return ResultStatusCode;
    }

What am I doing wrong here? What is the correct way of sending a file into REST endpoint ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2025-02-02 12:26:44

[From Form]期望使用应用程序/X-WWW-url-Formencoded接受接受标头。如果不是这种情况,请检查您的输出记录以查看为什么未处理请求。

[FromForm] expects an accept header with application/x-www-url-formencoded. If this is not the case, check your output-logs to see why the request is not processed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文