How to use RestRequest/RestResponse when the web service produces multipart form data in C#?

StackOverflow https://stackoverflow.com/questions/23322610

  •  10-07-2023
  •  | 
  •  

سؤال

I have a web service in a RESTful web server (java) which consumes media of type APPLICATION_FORM_URLENCODED and produces media of type MULTIPART_FORM_DATA. Now I'm working on a REST client (C#) and trying to use this web service. I'm using RestSharp as the REST client. My code goes as follows:

RestRequest request = new RestRequest("getDataFileChunkIS", Method.POST);
request.AddParameter("sessionId", sessionId);
request.AddParameter("dataFileId", dataFileId);            
request.AddParameter("offset", offset);
request.AddParameter("chunkSize", chunkSize);
request.AddParameter("checksumFlag", checksumFlag);

RestClient client = new RestClient(url);    
RestResponse response = (RestResponse)client.Execute(request);

But in this response I'm getting HTTP Status 406 - Not Acceptable. It says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." Maybe I'm doing it in a wrong way. So my question is that how can I execute this request whose response will contain MULTIPART_FORM_DATA ?

هل كانت مفيدة؟

المحلول

1) how can I execute this request whose response will contain MULTIPART_FORM_DATA?

request.AddHeader("Accept", "multipart/form-data")

2) how can I read this response header(contains JSON) using RestClient?

See answers to this question. Particularly the third one, which shows how to do it just with .NET 4.5 libraries.

You may need to implement IDeserializer to get access to the raw HttpResponse for consumption.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top