使用RestSharp发送HTTP POST Multipart/form-data字段 [英] Sending HTTP POST Multipart/form-data field using RestSharp

查看:1438
本文介绍了使用RestSharp发送HTTP POST Multipart/form-data字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将RestSharp用于REST API时遇到问题,我需要在正在处理的项目中使用它.我需要发出的请求分为三个部分:标头API密钥,要上传的文件和一堆JSON格式的数据. API要求使用表单字段名称"data"发送数据部分.由于某种原因,这会引起问题,因为它在请求的正文中命名了数据"字段.

I'm having issues using RestSharp for a REST API I need to use for a project I'm working on. The request I need to issue is in three parts: A header API key, a file to upload, and a bunch of data in JSON format. The API requires that the data part be sent using a form field name of "data". For some reason this is causing issues since it's naming the field "data" within the body of the request.

我的代码如下:

var request = new RestRequest(UPLOAD_DOC_URLSEGMENT, Method.POST)
{
    RequestFormat = DataFormat.Json,
    AlwaysMultipartFormData = true,
    JsonSerializer = new RestSharpJsonDotNetSerializer(customSerializer)
};

if (doc is DocA)
    request.AddParameter("data",doc as DocA,ParameterType.RequestBody);
    //request.AddBody(doc as DocA);
else
    request.AddParameter("data", doc as DocB,ParameterType.RequestBody);
    //request.AddBody(doc as DocB);

request.AddFile("file", doc.File.FullName);

如您所见,我尝试同时使用request.AddBody(doc)方法和request.AddParameter(name, object, type)方法.他们两个似乎都没有正确发送数据,因为我从服务器收到响应,说缺少必需的参数.使用fiddler,使用这两种方法都可以看到二进制数据,但是看不到JSON数据.我已经阅读过RestSharp文档,但是找不到任何可以将特定字段"名称指定为表单数据主体的数据"的内容,这是导致我遇到的问题的原因.我在这里做什么错了?

As you can see I've attempted to use both the request.AddBody(doc) method and the request.AddParameter(name, object, type) method. Neither of them appear to be sending the data properly because I receive a response from the server saying required parameters are missing. Using fiddler I can see the binary data, but never the JSON data with both of these methods. I've gone through the RestSharp documentation, but I can't find anything that allows me to specify a particular "field" name as "data" for the form data body, which is what I believe is causing the issue I'm having. What am I doing wrong here?

使用fiddler进行进一步检查后,似乎根本没有将我的JSON数据添加到HTTP请求的正文中.但是,在上载(执行命令)之前有一个断点,我可以看到所有在参数列表(和文件列表)中正确序列化的东西.用Fiddler检查时,我看到文件二进制数据,然后是多部分/表单数据边界,然后什么都没有.我会假设这是我的数据应该存在的地方...

Upon further inspection with fiddler it appears that it's not adding my JSON data at all to the body of the HTTP request. However, with a break point right before the upload (execute command) I can see everything serialized properly within the parameter list (and file list). When inspecting the with Fiddler I see the file binary data, and then a multipart/form-data boundary, and then nothing. I would assume this is where my data is supposed to be...

推荐答案

因此,我通过使用AddBody方法来解决此问题,该方法会自动杀死多部分表单图像,并且不会发送它们. 您必须改用add参数.

So I am doing this by working around a problem with using the AddBody method which automatically kills the multi part form images and will not send them. You must use add parameter instead.

要解决此问题,您可能需要在通讯的两边做一些工作.

To solve this problem you may have to do a little work on both sides of the communication.

要从客户端发送消息,请执行以下操作:

To send the message from the client you do the following:

new RestRequest("<Your URI>");
request.AddParameter("request", tokenRequest.ToJson());
request.AddFile("front", frontImage.CopyTo, "front");
request.AddFile("back", backImage.CopyTo, "back");
request.AddHeader("Content-Type", "multipart/form-data");

在我的Web服务端,我接受json作为方法的参数,并手动获取对文件流的引用:

On my web service side, I accept the json as the argument to the method and manually obtain a reference to the file streams:

public JsonResult MyService(StoreImageRequest request)
{
    var frontImage = HttpContext.Request.Files["front"].InputStream;
    var backImage = HttpContext.Request.Files["front"].InputStream;
}

这篇关于使用RestSharp发送HTTP POST Multipart/form-data字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆