Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I POST a image binary in retrofit 2.0 beta 2? #1217

Closed
enginebai opened this issue Oct 21, 2015 · 7 comments
Closed

How can I POST a image binary in retrofit 2.0 beta 2? #1217

enginebai opened this issue Oct 21, 2015 · 7 comments

Comments

@enginebai
Copy link

I'm trying to POST a image binary to my server, here is my code:

API Service

@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);

API Call

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.

@tomkoptel
Copy link

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.

@LintaoLin
Copy link

i have the same problem before,i think you can write Service like this:
@part("photo"; filename="picture_name.png"") RequestBody photo

@ayon115
Copy link

ayon115 commented Oct 27, 2015

Please check #1063

@enginebai
Copy link
Author

@LintonLin How can I specify picture_name.png in @Part("photo\"; filename="picture_name.png"") RequestBody photo?? I have to specify the filename dynamically.

@enginebai
Copy link
Author

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)

@enginebai
Copy link
Author

Final Solutions

  • API Service
@POST("trip/{tripId}/media/photos")
Call<MediaPost> postEventPhoto(
        @Path("eventId") int tripId,
        @Header("Authorization") String accessToken,
        @Query("direction") String direction,
        @Body RequestBody photo);
  • Caller
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);

@JakeWharton
Copy link
Member

Dupe of #1140 and #1137.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants