Search code examples
phprestposthttp-post

How to upload multiple files as part of a REST API


I am trying to create a REST API for my web service.

I want to make the users of the API able to initiate a new request with my service. This involves uploading one or two zip files along with some other parameters.

How can I make this all combined into one request? Or do I need to do it multiple requests somehow?

I don't have a lot of familiarity with making REST APIs so I don't know how people usually do it.

I'm using PHP for my site if that matters.


Solution

  • To do this, you'd need your client to upload in mime/multipart format. I don't know PHP, but I'm sure there's a library out there that will support receiving/parsing the multipart messages you get.

    As for whether it's a good idea .. If initiating the request is the creation of a single resource, it's not unreasonable to accept mime/multipart. If the parts being sent are themselves full-fledged resources, it would probably be better to make the client send them up separately, and reference them in the initiation request. Also note that mime/multipart is going to be a bit harder for your clients to deal with than simple requests.

    This post seems to be related to what you're trying to accomplish.