API to receive file
API to receive file
I've written a few simple web services that receive JSON requests from a client, parse the JSON using Scott's YAJL port, and perform the iSeries application tasks from there. Now I have a customer that wants to upload files to an API. I would need to receive the file (via multipart/form-data?), save it to an IFS directory, and submit a job to do some stuff with it. I'm having trouble figuring out how to write a web service to receive a file. Can anyone point me in the general direction for this? Thanks!
-
- Site Admin
- Posts: 763
- Joined: Sun Jul 04, 2021 5:12 am
Re: API to receive file
There are three basic ways to do this in REST
So to do option #1, you don't need YAJL -- you're sending a file that's NOT a JSON file.. it's just a raw string of bytes. You just save it to disk as-is.
For option #2, you'd need a tool that helps you with multipart documents. There are some routines for this in HTTPAPI, but they're mainly aimed at being on the calling end of things. A tool like MDRest4i (a commercial REST API tool that I work on for a living) does have tools for this, though. Once you've dealt with the multipart format (For example, if you wrote your own tool for it) you could use YAJL to process the JSON. A full REST API tool like MDRest4i will do both the multipart document and the JSON parsing for you.
For option #3, you could do it with my free tools, using my Base64 tool to encode the data and YAJL to put it in a JSON document... but again, this is a clunky old-fashioned way to do things.
I'd start by determining what sort of data you're processing, and what sort of application, then looking at what consumers you'll be interacting with and learning what would work best for them.
- You can just send a file over HTTP. This is the easiest way.
- You can send a file as an "attachment". This involves sending a "regular" file like JSON or XML, with another file "attached" to it using a MIME-style multipart document. An example would something like an employee ID picture, where you need a data file describing the employee (name, employee number, other record details) together with a picture.
- You can embed a document inside a JSON or XML file by encoding it with base64 or similar, and then putting the details in a large character field in the document. This is the least efficient way, and I don't recommend it... This was somewhat popular back in the days of SOAP APIs. I'd recommend using the attachment strategy instead of this, though.
So to do option #1, you don't need YAJL -- you're sending a file that's NOT a JSON file.. it's just a raw string of bytes. You just save it to disk as-is.
For option #2, you'd need a tool that helps you with multipart documents. There are some routines for this in HTTPAPI, but they're mainly aimed at being on the calling end of things. A tool like MDRest4i (a commercial REST API tool that I work on for a living) does have tools for this, though. Once you've dealt with the multipart format (For example, if you wrote your own tool for it) you could use YAJL to process the JSON. A full REST API tool like MDRest4i will do both the multipart document and the JSON parsing for you.
For option #3, you could do it with my free tools, using my Base64 tool to encode the data and YAJL to put it in a JSON document... but again, this is a clunky old-fashioned way to do things.
I'd start by determining what sort of data you're processing, and what sort of application, then looking at what consumers you'll be interacting with and learning what would work best for them.
Re: API to receive file
Thanks Scott! Option 1 would work fine for this purpose. I think I found an easy way... CGIDEV2 seems to handle it pretty much automagically, saving any uploaded file to a temp directory and creating a CGI variable with the filename.