Wednesday, August 22, 2018

Multipart Content with refit

Leave a Comment

I am using multipart with Refit. I try to upload profile picture for my service the code generated from postman is looking like this

var client = new RestClient("http://api.example.com/api/users/1"); var request = new RestRequest(Method.POST); request.AddHeader("Postman-Token", "xxx"); request.AddHeader("Cache-Control", "no-cache"); request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_method\"\r\n\r\nput\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"profile_picture\"; filename=\"ic_default_avatar.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody); IRestResponse response = client.Execute(request); 

and then I construct Refit method like this

[Multipart] [Post("/users/{id}")] IObservable<BaseResponse<User>> UpdateProfilePicture(int id,[AliasAs("profile_picture")] byte[] profilePicture,[AliasAs("_method")]string method="put"); 

if I use byte[] or ByteArrayPart it will throw exception

{System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error getting response stream (chunked Read2): ReceiveFailure ---> System.Exception: at System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String where) [0x00031] in :0 at System.Net.WebConnection.Read (System.Net.HttpWebRequest request, System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x000d2] in :0 at System.Net.WebConnectionStream.ReadAll () [0x0010e] in :0 at System.Net.HttpWebResponse.ReadAll () [0x00011] in :0 at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x001d6] in :0 at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x0013e] in :0 at System.Net.WebConnection.ReadDone (System.IAsyncResult result) [0x0024d] in :0 at System.Net.Sockets.SocketAsyncResult+<>c.b__27_0 (System.Object state) [0x00000] in :0 at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00015] in :0 at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in :0 at ObjCRuntime.Runtime.ThreadPoolDispatcher (System.Func`1[TResult] callback) [0x00006] in <0b60c1467e7449608ac42f9c7bbfdd05>:0 at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00009] in :0 at System.Net.WebConnection.HandleError (System.Net.WebExceptionStatus st, System.Exception e, System.String where) [0x00031] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:439

. and if I use Stream or StreamPart it will also throw exception said stream is closed.

This question has an open bounty worth +50 reputation from albilaga ending in 1 hour.

This question has not received enough attention.

You can use IEnumerable<StreamPart> to upload a files:

  [Multipart]   [Post("/users/{id}")]   Task UpdateProfilePicture(int id, [AliasAs("profile_picture")] IEnumerable<StreamPart> streams); 

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

0 comments:

Post a Comment