Saturday 6 August 2022

How to Design Mule API to process Attachments



This blog Explains , how to design Mule API to process attachments.
Quite often we get requirement to design API's that process attachment
Here is soloution for designin and developing API that capable of hadling attachments


  1. 1) RAML Design
  2. 2) Implementation of API
  3. 3) Testing API

1) RAML design:

#%RAML 1.0
title: attachment-api
mediaType: [application/json, multipart/form-data]

/attachments:
  post:
    description: Creates new attachment 
    body:
      multipart/form-data:
        properties:
          attachment-name: string
          attachment-description: string
          attachment-type: string
          content: string

    responses:
      200:
        body:
          application/json:
            example:
              {
                "id" : 101,
                "name" : "task-report"
              }

  /{id}:
    get:
      responses:
        200:
          body:
            application/json:
              example:
                   {
                   "id" : 101,
                    "name" : "task-report"
                   }
   
I want to take 4 properties of attachment, name, description, type and content. I designed as part of body of attachment.
we need input these properties while uploading attachment.

  
   
2) Implementation of API:
Create project by importing API from Design center 
To prepare attachment record from the input, I configured the transform message component as below
%dw 2.0
output json
---
{
name: payload.parts."attachment-name".content,
description : payload.parts."attachment-description".content,
"type": payload.parts."attachment-type".content,
content : payload.parts."content".content
       
}
I am storing attachments in DB. the table structure as below . 
CREATE TABLE `attachment` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`description` varchar(45) DEFAULT NULL,
`type` varchar(45) DEFAULT NULL,
`content` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


  Please check attahed project for complete Mule source code

Run and deploy the code .

1) Create attachment: 

http://localhost:8081/api/attachments

Database record: 


2) Retrieve record:


Project code: 
https://github.com/saisrinu28/attachment-api.git

Thank you. 
















How to Design Mule API to process Attachments

This blog Explains , how to design Mule API to process attachments. Quite often we get requirement to design API's that process attachme...