FubarDevelopment / restsharp.portable

Some kind of a RestSharp port to PCL
BSD 2-Clause "Simplified" License
96 stars 33 forks source link

Cannot use AddBody and AddFile at the same time. #89

Closed mikevmil closed 7 years ago

mikevmil commented 8 years ago

When I use AddBody and AddFile in the same request, the request fails.

var request = new RestRequest(Method.POST);
request.Resource = "TestEndpoint";
request.AddParameter("queryParam", "queryVal", ParameterType.QueryString);       

//Works but don't want to use this since this will always output my json object as a parameter
request.AddParameter("answers", JsonConvert.SerializeObject(answerDictionary));

//What I need, but doesn't work when file has been added since Name of the key is always null or (since 4.x) an empty string.
request.AddBody(MyObject);
//or request.AddJsonBody(MyObject);

request.AddFile("fileParam", file, "filename.jpg");
return await Execute<Result>(request);

My stacktrace:

at System.Net.Http.MultipartFormDataContent.Add (System.Net.Http.HttpContent content, System.String name) [0x00012] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/System.Net.Http/System.Net.Http/MultipartFormDataContent.cs:56 
at RestSharp.Portable.HttpClient.RestClientExtensions.GetMultiPartContent (RestSharp.Portable.IRestClient client, RestSharp.Portable.IRestRequest request, RestSharp.Portable.RequestParameters parameters) [0x001c3] in <bf5e823f8e0b4e6aa7aed00c39d700ac>:0 
at RestSharp.Portable.HttpClient.RestClientExtensions.GetContent (RestSharp.Portable.IRestClient client, RestSharp.Portable.IRestRequest request, RestSharp.Portable.RequestParameters parameters) [0x0003f] in <bf5e823f8e0b4e6aa7aed00c39d700ac>:0 
at RestSharp.Portable.HttpClient.RestClient.GetContent (RestSharp.Portable.IRestRequest request, RestSharp.Portable.RequestParameters parameters) [0x00001] in <bf5e823f8e0b4e6aa7aed00c39d700ac>:0 
at RestSharp.Portable.RestClientBase+<ExecuteRequest>d__67.MoveNext () [0x00182] in <6ae3baac43704544b07146969630a0cf>:0

I debugged the call and it seems that there's no parameter set for the body when posting multipart data since MultipartFormDataContent.Add is called without a name.

It should be possible in the original RestSharp, considering someone else got it to work: #https://github.com/restsharp/RestSharp/issues/524#issuecomment-68075428 What @tyrsius is doing at attempt 1 is exactly what I want to happen.

Am I doing something wrong or is it a bug?

kyeotic commented 8 years ago

You're url is broken when clicked, but it works when copy/pasted

mikevmil commented 8 years ago

@tyrsius Thanks for noticing! I've updated the link.

fubar-coder commented 7 years ago

The body parameter needs to be named. Try using:

AddParameter(new Parameter { Name = "name", Type = ParameterType.RequestBody, Value = yourValue }

The next release contains some convenience functions for this situation.

mikevmil commented 7 years ago

Thanks!