简体   繁体   中英

Restsharp how to specify ContentType for a parameter in a MultiPart/form-data Request

I'm trying to send a document to a REST service using RestSharp. The request must be a multipart/form-data request with two parameters. The first one, with the name "request" must have content-type/application-JSON.

I'm using the code below but I don't understand how to force this content-type only for this parameter. If I go throw the debug session the value of para.content-type is always "null".

var request = new RestRequest("/sendDocument")
{
   AlwaysMultipartFormData = true
};
var param = Parameter.CreateParameter("request", sendDocRequestObjectRequest, ParameterType.GetOrPost);
request.AddParameter("request", param, ParameterType.GetOrPost);
request.AddFile("files", @"c:\test.pdf");
var response = restClient.Post(request);

Using Postman, I can specify the content-type "multipart/form-data" for the Header and "application-JSON" only for the parameter. In this case, everything is working fine. How can I have the same behaviour as Postman using Restsharp?

RestSharp will automatically use the multipart form content type when you add a file.

If your request doesn't contain files, and you still want to post using a multipart form, you can set request.AlwaysMultipartFormData property to true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM