How to fill Content-Type data within POST Request

System Information
  • Strapi Version: 3.4.0
  • Operating System: macOS Big Sur 11.1
  • Database: SQLite
  • Node Version: 14.15.1
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.10

I want to fill content-types with data within a POST Request, because I want data to be added outside of the Strapi Backend. In this example I have the content type “article” and want to set the “title” to “Hello World” after the post request.

I already tried overwriting the controller like this:

  async create(ctx) {
    let entity;

    if (ctx.is('multipart')) {
      const {data, files} = parseMultipartData(ctx);
      entity = await strapi.services.article.create(data, {files});
    } else {
      entity = await strapi.services.article.create(ctx.request.body);
    }

    const article = await sanitizeEntity(entity, {model: strapi.models.article});
    article.title = 'Hello World';
    console.log(article);
    return article;
  },

The console log prints out the right object, but when I look into the backend, the ‘Hello World’ is not inside the text field.

article, in this case, contains the RESULT object of the creation.

The correct code:

 async create(ctx) {
     let entity = await strapi.services.article.create({
         ...ctx.request.body,
         title:'Hello World'
     });
     return sanitizeEntity(entity, {
         model: strapi.models.article
     });
 }
2 Likes