0

I obtained a upload uRL Programmatically from my deployed google app., however i keep getting the below response...what type of parameter am i missing?

Bad content type.  Please use multipart.
2016-08-16 09:25:34 ERROR PayitWebClientApp:207 - 400 Bad Request
Bad content type.  Please use multipart.
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1054)
    at com.vanitysoft.payit.PayitInvokeAuthApp.generateUploadUr(PayitInvokeAuthApp.java:198)
    at com.vanitysoft.payit.PayitInvokeAuthApp.main(PayitInvokeAuthApp.java:217)

This is the code:

      MultipartContent content = new MultipartContent().setMediaType(
                new HttpMediaType("multipart/form-data")
                        .setParameter("boundary", "__END_OF_PART__"));

        FileContent fileContent = new FileContent(
               "multipart/related", file);
        MultipartContent.Part part = new MultipartContent.Part(fileContent);
        part.setHeaders(new HttpHeaders().set(
                "Content-Disposition", 
                String.format("form-data; name=\"content\"; filename=\"%s\"", file.getName())));
        content.addPart(part);

      httpRequest = httpRequestFactory.buildPostRequest(new GenericUrl(uploadItem.getUploadUrl()) ,fileContent);     
      LOGGER.info("upload...[" + uploadItem.getUploadUrl() +"]");
      httpResponse = httpRequest.execute();
      LOGGER.info("Upload complete");
      Assert.isTrue(httpResponse.getStatusCode() == 200, IOUtils.toString(httpResponse.getContent()));      

      LOGGER.info( "Success[" + httpResponse.getContent() +"]" );
Jeryl Cook
  • 989
  • 17
  • 40

2 Answers2

0

You are missing enctype="multipart/form-data" on <form> tag.

Check this answer: Why is form enctype=multipart/form-data required when uploading a file?

Community
  • 1
  • 1
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
  • Hi, my client is a java app. what do you mean add that to 'form tag'? is do i add this to the Content-Diposition header? – Jeryl Cook Aug 16 '16 at 13:43
0

Opps, typo:, i needed to use 'content' not fileContent

httpRequest = httpRequestFactory.buildPostRequest(new GenericUrl(uploadItem.getUploadUrl()) ,content);

Jeryl Cook
  • 989
  • 17
  • 40