I have an external API POSTing data to my project. I want to move my project to Strapi. But ofcourse, the shape of that external POST does not match the data creation endpoint of my Strapi model. Also it's in XML so I need to parse it fist and need to mutate the incoming data to match the model better. How should one go about.
My thoughts include:
strapi.query('myModel').create({})
I would love to hear some ideas and concepts from people familiar with Strapi.
So for the answer to this question.
You will have to use this concept - https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#core-controllers
Customizing your create
controller function.
And at the beginning of the function, you will have to check the format of ctx.request.body
.
If the content has an XML format, in this case, you will have to convert it in JSON.
Path — api/**/controllers/**.js
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
module.exports = {
/**
* Create a record.
*
* @return {Object}
*/
async create(ctx) {
// if ctx.request.body is XML
// ctx.request.body = convertXMLtoJSON(ctx.request.body);
// you will have to find a code that convert XML to JSON
// and simply add id in this function
let entity;
if (ctx.is('multipart')) {
const { data, files } = parseMultipartData(ctx);
entity = await strapi.services.restaurant.create(data, { files });
} else {
entity = await strapi.services.restaurant.create(ctx.request.body);
}
return sanitizeEntity(entity, { model: strapi.models.restaurant });
},
};