I'm trying to POST a image binary to my server, here is my code:
@Multipart
@Headers({
"Content-Type: application/octet-stream"
})
@POST("trip/{id}/media/photos")
Call<MediaPost> postMediaPhoto(
@Path("id") int id,
@Header("Authorization") String accessToken,
@Query("type") String type,
@Part("photo") RequestBody photo);
RequestBody requestBody = RequestBody
.create(MediaType.parse("application/octet-stream"), media.getPath());
Call<MediaPost> mediaPostCall = eventApiService.postMediaPhoto(
id,
((GlobalInfo) getApplicationContext()).getApiAccessToken(),
type,
requestBody);
Response<MediaPost> mediaPostResponse = mediaPostCall.execute();
I've tried to use MediaType.parse("image/*") or MediaType.parse("application/octet-stream"), but the server always returned No MIME found. I'm sure that my file exists!!
How can I fix it?? Thanks.
Hello, can you provide dump of network calls? You can do this in multiple ways. One of suggestions to use proxy network tool Charles. Or simply use OkHttp logging interceptor.
Full request and response info may help to understand your issue.
i have the same problem before,i think you can write Service like this:
@Part("photo\"; filename="picture_name.png"") RequestBody photo
Please check #1063
@LintonLin How can I specify picture_name.png in @Part("photo\"; filename="picture_name.png"") RequestBody photo?? I have to specify the filename dynamically.
Here is my http interceptor logging:
10-30 10:42:40.988 21588-24651/com.styletrip.api D/OkHttp: --> POST /api/trip/199/media/photos?direction=portrait HTTP/1.1
10-30 10:42:40.988 21588-24651/com.styletrip.api D/OkHttp: --73bbb958-c452-45e6-8ddb-d407d33225ee
10-30 10:42:40.988 21588-24651/com.styletrip.api D/OkHttp: Content-Disposition: form-data; name="photo"
10-30 10:42:40.988 21588-24651/com.styletrip.api D/OkHttp: Content-Transfer-Encoding: binary
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp: Content-Type: image/*; charset=utf-8
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp: Content-Length: 48
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp:
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp: /storage/sdcard1/Pictures/LINE/1444450744226.jpg
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp: --73bbb958-c452-45e6-8ddb-d407d33225ee--
10-30 10:42:40.989 21588-24651/com.styletrip.api D/OkHttp: --> END POST (273-byte body)
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: <-- HTTP/1.1 400 Bad Request (54ms)
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Server: nginx/1.6.2
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Date: Fri, 30 Oct 2015 02:42:41 GMT
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Content-Type: application/json; charset=utf-8
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Content-Length: 75
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Connection: keep-alive
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: X-Powered-By: Express
10-30 10:42:41.043 21588-24651/com.styletrip.api D/OkHttp: Access-Control-Allow-Origin: *
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: Access-Control-Max-Age: 1209600
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: ETag: W/"4b-/ybqNvLDr3e8XAe8sLMIyg"
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: OkHttp-Selected-Protocol: http/1.1
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: OkHttp-Sent-Millis: 1446172960997
10-30 10:42:41.044 21588-24651/com.styletrip.api D/OkHttp: OkHttp-Received-Millis: 1446172961043
10-30 10:42:41.048 21588-24651/com.styletrip.api D/OkHttp: {"name":"ParameterError","message":"Invalid Parameters No MIME","code":400}
10-30 10:42:41.048 21588-24651/com.styletrip.api D/OkHttp: <-- END HTTP (75-byte body)
@POST("trip/{tripId}/media/photos")
Call<MediaPost> postEventPhoto(
@Path("eventId") int tripId,
@Header("Authorization") String accessToken,
@Query("direction") String direction,
@Body RequestBody photo);
InputStream in = new FileInputStream(new File(media.getPath()));
byte[] buf;
buf = new byte[in.available()];
while (in.read(buf) != -1);
RequestBody requestBody = RequestBody
.create(MediaType.parse("application/octet-stream"), buf);
Call<MediaPost> mediaPostCall = tripApiService.postTripPhoto(
tripId,
((GlobalInfo) getApplicationContext()).getApiAccessToken(),
direction,
requestBody);
Dupe of #1140 and #1137.
Most helpful comment
Final Solutions