【実装環境】
- Xamarin.Forms 2.5.0.122203
- PCL Profile7
- Android の HttpClient を使用
【やりたいこと】
Xamarin で、HttpClient を用いてマルチパートなデータを送信したいです。
実現したいのは以下のcurlになります。
curl
1curl -v -H "Content-Type:multipart/form-data" 2-F "parameters={\"communityDetail\":\"world\",\"communityName\":\"hello\",\"communityTags\":\"#hello\"};type=application/json" 3-F "file=@/Users/Daiki/Desktop/cat06.jpg" 4https://app-server.mybluemix.net/api/v1/communities
【試したコード】
以下のコードを試しました。
C#
1var content = new MultipartFormDataContent(); 2content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); 3 4var imageContent = new ByteArrayContent(imageByteArray); 5imageContent.Headers.ContentDisposition = 6 new ContentDispositionHeaderValue("form-data") {Name = "\"file\""}; 7imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg"); 8content.Add(imageContent, "file"); 9 10var bytes = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(community)); 11var byteContent = new ByteArrayContent(bytes); 12byteContent.Headers.ContentDisposition = 13 new ContentDispositionHeaderValue("form-data") {Name = "\"parameters\""}; 14byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 15content.Add(byteContent, "parameters"); 16 17var response = await _client.PostAsync("api/v1/communities", content); 18Debug.WriteLine(response.StatusCode + ", " + response.Content.ReadAsStringAsync().Result);
結果、以下のレスポンスが返っています(サーバーサイドは Spring Boot)。
InternalServerError, {"timestamp":1521470466137,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.multipart.MultipartException","message":"Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found","path":"/api/v1/communities"}
マルチパートなデータが正しく送れていないように見えますが、何か間違っているところがありますでしょうか?
ご教授のほど、よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/20 13:47