Simon
Simon

Reputation: 417

Blob Store responds with 415 Unsupported Media Type

I have an API running on Google App Engine that receives images via mail, trough App Engines incoming mail feature.

This means that I can not use Blob Store with JSP, as Google describes here as a typical use-case. Instead I upload the image to Blob Store using Retrofit.

This works, as the image gets uploaded to Blob Store, but Blob Store responds with 415 Unsupported Media Type. I've also tried to upload other file types and by using Postman, but Blob Store keeps responding with 415 Unsupported Media Type even for successful requests.

Retrofit

MailReceiver

String uploadUrl = BlobstoreServiceFactory.getBlobstoreService()
                  .createUploadUrl("/api/camera/blobstore-response");
uploadUrl = uploadUrl.split("appspot.com/")[1]; // Remove the base URL.


TypedOutput body = new TypedOutput() {
  @Override
  public String fileName() {
    return fileName;
  }

  @Override
  public String mimeType() {
    return "image/jpeg";
  }

  @Override
  public long length() {
    return -1;
  }

  @Override
  public void writeTo(OutputStream outputStream) throws IOException {
    ByteStreams.copy(inputStream, outputStream);
  }
};

blobStoreApiService.uploadImage(uploadUrl, body);

BlobStoreApiService

  @Multipart
  @POST("/{path}")
  Object uploadImage(@Path(value = "path", encode = false) String uploadPath, @Part("file") TypedOutput image);

Postman

Works, but receives 415 Unsupported Media Type Working but receives <code>415 Unsupported Media Type</code>

Identical to the request above, but added Content-Type header. This does not work (400 Bad Request) Identical to the request above, but added <code>Content-Type</code> header

Upvotes: 1

Views: 1805

Answers (1)

Simon
Simon

Reputation: 417

When calling the Blob Store to get an URL to where you should make your upload request, you add a callback URL (in this case /api/camera/blobstore-response) to a handler that Blob Store calls when the Blob has been saved. Like this:

String uploadUrl = BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/api/camera/blobstore-response");

So when Blob Store responds with 415 Unsupported Media Type, that's actually the response Blob Store got from the callback handler, ie. the response that Blob Store got when calling api/camera/blobstore.

The solution is to create a handler that returns 2XX and point Blob Store to that endpoint. This endpoint have to handle a POST request containing multipart form data.

Upvotes: 2

Related Questions