我对我的项目有一个外部的API POSTing数据。我想把我的项目移到Strapi。但是当然,外部POST的形状与我的Strapi模型的数据创建端点不匹配。而且,它是XML的,所以我需要首先解析它,并需要对传入的数据进行变异以更好地匹配模型。一个人该怎么做。
我的想法包括:
strapi.query('myModel').create({})
我想听听熟悉Strapi的人的一些想法和概念。
发布于 2020-04-21 04:58:14
所以对于这个问题的答案。
您必须使用这个概念- https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#core-controllers
自定义您的create
控制器功能。
在函数开始时,您必须检查ctx.request.body
的格式。
如果内容具有XML格式,在本例中,您必须用JSON转换它。
路径- 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 });
},
};
https://stackoverflow.com/questions/61168599
复制相似问题