Reputation: 242
I'm trying to upload an image with uploading bytes of Google Photos API .
So here is my request with OkHttp3:
Request request = new Request.Builder()
.addHeader("Authorization", "Bearer " + token)
.addHeader("Content-type:", "application/octet-stream")
.addHeader("X-Goog-Upload-Content-Type:",mimeType)
.addHeader("X-Goog-Upload-Protocol:", "raw")
.url("https://photoslibrary.googleapis.com/v1/uploads")
.post(requestBody) // how to build request body?
.build();
the documentation says: "In the request body, include the binary of the file:"
media-binary-data
What does it means?
For a given file, I assume that it is:
byte[] data = FileUtils.readFileToByteArray(new File(myPath));
But how do you build this requestBody with data array?
EDIT I already tried :
MediaType mType = MediaType.parse("application/octet-stream; charset=utf-8");
RequestBody requestBody = RequestBody.create(mType,new File(path));
Exception :
okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR
Thank you in advance for putting me on the track!
Upvotes: 3
Views: 3221
Reputation: 242
unfortunately I left the ":" after the parameter keys !!!!
Request request = new Request.Builder()
.addHeader("Authorization", "Bearer " + token)
.addHeader("Content-type", "application/octet-stream")// and no X-Goog-Upload-Content-Type :
.addHeader("X-Goog-Upload-Content-Type",mimeType)//same
.addHeader("X-Goog-Upload-Protocol", "raw")//same
.url("https://photoslibrary.googleapis.com/v1/uploads")
.post(requestBody)
.build();
And for the requestBody:
byte [] data = FileUtils.readFileToByteArray(file);
RequestBody requestBody = RequestBody.create(mimeTypeOfFile,data);
Upvotes: 0
Reputation: 3569
RequestBody
has a few overloaded factory methods:
It looks like you're supplying the second parameter in either case correctly.
As for the MediaType
parameter, I usually use the MIME type of the underlying image rather than specifying general binary content, e.g.:
MediaType.parse("image/jpeg")
or
MediaType.parse("image/png")
...
Upvotes: 1