0

I am looking to test my REST resource on dropwizard using CURL command. I am able to upload the file and get the content and file name. But along with the file I want to pass some list of Ids as well.

  1. How can I pass list of long ids (array of Long data type) to the REST service in the below CURL command?
  2. Can I pass a json structure additionally in the below command if required along with my file content?

CURL command

curl -F 'file=@/cygdrive/c/TestDocument1.txt' http://localhost:8199/test-app/api/upload-documents/1004/documents

REST service to upload file

 @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Path("/reconciliation-details/{reconciliationDetailId}/documents")
    public Response uploadDocument(@FormDataParam("file") File inventoryDocumentContent,
                                            @FormDataParam("file") FormDataContentDisposition fileDetail,
                                            @FormDataParam("reconciliationIds") List<Long> reconciliationIds) throws Exception {
            byte[] documentContent = FileUtils.readFileToByteArray(inventoryDocumentContent);
            String documentName = fileDetail.getFileName();
            reconDetailsService.uploadDocument(documentName, documentContent, reconciliationIds);           
        return ResponseHelper.createOkResponse();
    }
serah
  • 2,057
  • 7
  • 36
  • 56

2 Answers2

0

You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring's side.

Looking at the curl man page, I think you can use -H:

-H "Content-Type: application/json"

Full example:

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

(-H is short for --header, -d for --data.)

For Json with file

curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith@gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/
Rahul khanvani
  • 373
  • 3
  • 13
  • I would need to send content type as multipart/form-data since I am uploading a file and sending its content as a form data payload. So my question is how can I in addition to the form data content send list of ids or json structure? – serah Jul 11 '17 at 09:51
0

With multipart, each part can have it's own Content-Type header. By default if it is not specified, text/plain is used. Some clients cannot set the Content-Type for individual parts, as mentioned in this post (for which there is a solution mentioned), but cURL has this capability. You just need to append ;type=<content-type>. For example

-F "reconciliationIds=[1, 2, 3, 4];type=application/json"

Instead of the using the actual JSON array, you could also use a file containing the JSON

-F "reconciliationIds=@path-to-json;type=application/json"

Jersey will see that the Content-Type for this part is application/json and use the deserializer that it would normally use for JSON

Make sure to see the linked post above - Even though this may work, you don't want to limit your users to only using clients with this capability. The bottom of the post gives you the solution, though I have not tested it with a List<Long>. I'm not sure how that would work using the getEntityAs(Class), as you wouldn't be able to pass the generic Long type. You could always wrap it in a POJO instead of just using a list.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720