0

I have an api that will be accessed by a URI like this:

POST: http://<domain>/app/rest/colors

This post request will send some string parameters (i.e name: "red") along with a file. Ideally I would like the data given to the API in JSON format but if there isn't a way to pass a file to JSON then I'm open to other formats as well.

Currently, when I'm taking parameters from a form post, my controller action looks kind of like this:

def save() {
    def colorInstance = new Color(params)
    CommonsMultipartFile file = request.getFile('filename')
    fileUploadService.upload(file)
    if (colorInstance.save(flush: true)) {
            flash.message = "Created"
            redirect(action: "list")
    }
    else {
        render(view:  "create", model: [colorInstance: colorInstance])
    }
}

Question

  • How can a file be passed to the REST API? Can it still be JSON?
  • How can I access that file in my save action
  • How can I test the action with curl

For example typically I do

curl -XPOST http://<domain>/app/rest/colors -d '{
       "name": "red",
       "shade": "light"
}'

But now I would like to send a file as well along with those two parameters

birdy
  • 9,286
  • 24
  • 107
  • 171

2 Answers2

1

if you are working with Jersey, than on the server side you should do:

@POST
@Path("/upload/user/{email}/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public ErrorDO uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail,
    @PathParam("email") String email, @Context HttpServletRequest hsr) {

    String name=fileDetail.getFileName();
      // TODO now just read from the inputstream and do what you want with it
    }

about the client side, its a simple post request, you can read about it here or any place else

Community
  • 1
  • 1
Dima
  • 8,586
  • 4
  • 28
  • 57
0

I tried to answer a similar question here. I think this can be of help. This is in context to Grails.

UPDATE
Content-type in the header keeps track of the request that a JSON request-body is attached to it. You can then access the JSON request body as request.JSON in grails controller.

curl -XPOST -H "Content-Type: application/json" -H "Accept: application/json" http://<domain>/app/rest/colors -d '{
       "name": "red",
       "shade": "light"
}'

In order to send a raw file in the POST request with the JSON payload, the curl command can be modified as

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" http://<domain>/app/rest/colors -d '{
           "name": "red",
           "shade": "light"
    }' -F myFile=@pathTosomefile`

and can be accessed in the controller as request.getFile('myFile')

Community
  • 1
  • 1
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Thanks, I came across that post and the linked post as well. However, none talk about file upload. I guess I'm having trouble understanding how would I test the API from curl and passing JSON data? I'll update the question with this as well... – birdy May 09 '13 at 12:52
  • Have you used `-H` for the `content-type` setting to `application/json` in the header while curling? Refer my update. – dmahapatro May 09 '13 at 13:23
  • FYI: I also use `REST Console` extension in Chrome for testing RESTful API. That can be a useful and intuitive tool as well. – dmahapatro May 09 '13 at 13:31
  • Great. the curl example you provided....How would I upload a file along with that? I'm currently processing the json being sent but now need to accept files as well... – birdy May 09 '13 at 13:57
  • You can use `-F myFile=@somefile` in `curl` command and access it in the `controller` by using `request.getFile('myFile')`. I will update my answer as well. – dmahapatro May 09 '13 at 14:04
  • I'm still having issues sending a file along with json data to my rest api. the curl command gives a warning saying: "Warning: You can only select one HTTP request!" I also tried REST Console but if I attach a file to the request then request header automatically become `application/xml` – birdy May 14 '13 at 18:36
  • It will be helpful if I can see exactly what you are trying in REST console? Any snapshot or process flow etc? – dmahapatro May 14 '13 at 18:55
  • Sure, I'll post some links to screenshots. Essentially after reading few answers (http://stackoverflow.com/questions/9081079/rest-http-post-multipart-with-json and http://stackoverflow.com/questions/16550561/rest-api-design-sending-json-data-and-a-file-to-the-api-in-same-request) I think I need `multipart/mixed` request with one part as json and other part as file. I don't know how to do this in rest console. Screen Shot: http://postimg.org/image/g61q4z99z/ Notice I'm trying to send a file and JSON – birdy May 14 '13 at 22:40