File REST API in RAML

RAML, also known as RESTful API Modeling Language makes it easy to define APIs that are human friendly. A customer asked me how to model an HTTP multipart request using RAML so I put together this example.

Just some background, an HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server. RAML provides a type file which can be used in combination with fileTypes to define what a user can upload over to an HTTP Server.

GitHub Project – File REST API

The link below provides a GitHub repo with an example project used in this article.

https://github.com/djuang1/file-rest-api-mule4

RAML Snippet

The RAML snippet below shows an example of the POST request to allow the upload of a multipart/form-data file. A successful 200 request returns a JSON payload with the status, file ID, and filename of the file passed to the API.

POST Example

Example Mule App

The example Mule app written for Mule 4.x provides an overview on how to get the filename from the uploaded file. It also writes the file to a directory specified in the properties file.

Test the API using Postman

Use Postman to make the POST request. When making the POST request, set the body to form-data and make sure the key for the file is named fileName


Posted

in

by

Tags:

Comments