0

I generated this code snippet from Postman and wanted to use it in Talend, but I don't know how to set the filename so that it pulls from the local drive. Here is the code:

OkHttpClient client = new OkHttpClient();

File sourceFile = new File("/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png");

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"uploadedFile\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001--");
Request request = new Request.Builder()
  .url("https://secret.attask-ondemand.com/attask/api-internal/upload/?apiKey=secret")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();
String json = response.body().string();
System.out.println(json.toString());

This is the local file I want to reference in [object Object]: "/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png"

Been chewing at this for hours with no luck. Any help is much appreciated.

John McCoy
  • 45
  • 1
  • 7
  • Does this compile? If so, what happens when you run it? – Brian Pipa Oct 03 '16 at 20:01
  • It does. Since the file isn't added to the RequestBody, the post just creates a shell with no file. – John McCoy Oct 03 '16 at 20:06
  • This is the result of the post: { "data": { "handle": "811bc97b1d16493e9e7caea123f06b7b" } } – John McCoy Oct 03 '16 at 20:06
  • And this is the result of checking to see if the file uploaded: { "error": { "class": "com.attask.common.AtTaskException", "message": "No file or directory name", "title": null, "msgKey": "exception.attask", "attributes": [ "" ], "code": 0 } } – John McCoy Oct 03 '16 at 20:07
  • What are you getting for response codes? – Brian Pipa Oct 03 '16 at 20:09
  • 200 OK on the first. 400 Bad Request on the second. – John McCoy Oct 03 '16 at 20:11
  • 1
    I've never used that library but looking at the javadocs (highly recommended): http://square.github.io/okhttp/3.x/okhttp/okhttp3/RequestBody.html it looks like you may want to do create(MediaType contentType, File file) - that's where you can pass in the File – Brian Pipa Oct 03 '16 at 20:13
  • And googling brought me to this: http://stackoverflow.com/questions/23512547/how-to-use-okhttp-to-upload-a-file – Brian Pipa Oct 03 '16 at 20:15
  • Thanks BrianPipa. That was enough to get my brain functioning again. – John McCoy Oct 03 '16 at 20:42

1 Answers1

0

Thanks for the help BrianPipa. With Workfront in particular, the form key has to be "uploadedFile" and the value needs to match the referenced file. Using MultipartBody, it is easier to build the request body as opposed to using string. Final code:

File sourceFile = new File("/Users/secret/Desktop/temp/16-27513/Digital Storefront Receipt.png");

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

RequestBody body = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)                                     
                   .addFormDataPart("uploadedFile", "Digital Storefront Receipt.png", RequestBody.create(MEDIA_TYPE_PNG ,sourceFile))
                   .build();

Request request = new Request.Builder()
  .url("https://secret.attask-ondemand.com/attask/api-internal/upload/?apiKey=secret")
  .post(body)
  .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
  .addHeader("cache-control", "no-cache")
  .build();

OkHttpClient client = new OkHttpClient();

Response response = client.newCall(request).execute();
String json = response.body().string();
System.out.println(json.toString());
John McCoy
  • 45
  • 1
  • 7
  • Could You please post the whole flow? I got stuch with _okhttpclient cannot be resolved to a type_ – darth0s Jul 12 '18 at 11:04