API to receive file

Any IBM i topic that does not fit in another forum
Post Reply
BA_RTC
Posts: 2
Joined: Mon May 06, 2024 5:40 pm

API to receive file

Post by BA_RTC »

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!
Scott Klement
Site Admin
Posts: 799
Joined: Sun Jul 04, 2021 5:12 am

Re: API to receive file

Post by Scott Klement »

There are three basic ways to do this in REST
  1. You can just send a file over HTTP. This is the easiest way.
  2. 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.
  3. 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.
YAJL is a tool for generating/interpreting JSON. So if you're sending a JSON document, YAJL might solve one piece of the puzzle for you -- but it's not going to help with sending files in the general sense. That's just not it's purpose.

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.
BA_RTC
Posts: 2
Joined: Mon May 06, 2024 5:40 pm

Re: API to receive file

Post by BA_RTC »

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.
Post Reply