Rest 带有正文和压缩数据的POST请求
我想在POST请求中发送一个zip(.txt)文件以及请求中的一些JSON参数。 post请求的示例请求和标题应该是什么。 在POST URL中添加varibales是正确的方法吗? 例如:POST Example.com/v1/name/{name}/Phone/{Phone}/date/{date}/upload [test.txt]Rest 带有正文和压缩数据的POST请求,rest,file,upload,Rest,File,Upload,我想在POST请求中发送一个zip(.txt)文件以及请求中的一些JSON参数。 post请求的示例请求和标题应该是什么。 在POST URL中添加varibales是正确的方法吗? 例如:POST Example.com/v1/name/{name}/Phone/{Phone}/date/{date}/upload [test.txt] 请建议。您应该将文件转换为字节数组,并将其作为请求正文参数添加到请求中。使用.NET的RestSharp,以下是我如何将文件添加到请求中的: RestRequ
请建议。您应该将文件转换为
字节
数组,并将其作为请求正文参数添加到请求中。使用.NET的RestSharp
,以下是我如何将文件添加到请求中的:
RestRequest request = new RestRequest("api/someEndpoint", Method.POST);
request.AddHeader("Content-Type", "text/plain"); // optional: depends on API
request.AddHeader("File-Name", fileName); // optional: depends on API
// add the file here. you need to provide the absolute path to it
request.AddParameter("text/plain", File.ReadAllBytes(absolutePathToYourFile), ParameterType.RequestBody);
注意:根据您关于使用
.txt
文件的评论,我使用了内容类型:text/plain
。但是,如果您的文件扩展名是.zip
,则需要使用应用程序/zip
作为内容类型
参数。您应该将文件转换为字节
数组,并将其作为请求正文参数添加到请求中。使用.NET的RestSharp
,以下是我如何将文件添加到请求中的:
RestRequest request = new RestRequest("api/someEndpoint", Method.POST);
request.AddHeader("Content-Type", "text/plain"); // optional: depends on API
request.AddHeader("File-Name", fileName); // optional: depends on API
// add the file here. you need to provide the absolute path to it
request.AddParameter("text/plain", File.ReadAllBytes(absolutePathToYourFile), ParameterType.RequestBody);
注意:根据您关于使用.txt
文件的评论,我使用了内容类型:text/plain
。但是,如果您的文件扩展名是.zip
,则需要使用应用程序/zip
作为内容类型
参数