How to create draft entry with strapi v4?

System Information
  • Strapi Version: 4.14.5
  • Operating System: Mac OS
  • Database: MySQL 8
  • Node Version: v18.14.2
  • NPM Version: 9.5.0
  • Yarn Version: 1.22.19

Hi,

I found this topic in forum.

Then I try to create an Entry with null publishedAt, but I got an error response.

Invalid format, expected a timestamp or an ISO date

Any other tips to create draft entries?

To create a Draft entry, just append status:'draft' along with the other body params.

And to create a Published entry, add status:'published' and publishedAt:YYYY-MM-DD HH:mm:ss along with other body params.

Hope this helps you :slight_smile:

2 Likes

To create a Draft entry, just append status:'draft' along with the other body params.

Unfortunately, it doesn’t works.
I use REST API from Ruby.
Send request with body like this.

    options[:body] = {
      data: {
        title: post.fields[:title],
        address: post.fields[:address],
        pinned: !!post.fields[:pinned],
        oldId: post.sys[:id],
        status: 'draft', # add this line
      }
    }

That is what I meant above. Anyways you have the solution now.

Happy coding :slight_smile:

Can you work it with strapi v4?

I saw database, but it has no status field and only published_at field presents.
Need any plugin or anything?

It works in Strapi V4. There won’t be any field for status in the DB.

If you do not submit status: ‘draft’ appended with you form body params, then strapi will consider that record as Published by default.

It is mandatory to pass status: 'draft' in order to create a Draft entry.

Thanks, but I can’t do with it.
No other settings required?

require 'httparty'

strapi_api_key = 'MY_API_KEY'
strapi_base_endpoint = 'http://localhost:1337'

headers = {
  "Authorization" => "Bearer #{strapi_api_key}"
}

options = {}
options[:headers] = headers

options[:body] = {
  data: {
    title: 'test',
    status: 'draft', # or 'Draft'
  }
}

res = HTTParty.post(strapi_base_endpoint + '/api/foos', options)
p res.body # returns "{\"data\":{\"id\":4,\"attributes\":{\"title\":\"test\",\"createdAt\":\"2023-12-21T08:51:40.266Z\",\"updatedAt\":\"2023-12-21T08:51:40.266Z\",\"publishedAt\":\"2023-12-21T08:51:40.266Z\"}},\"meta\":{}}"

This is solution that worked for my custom APIs.

Then I try to create an Entry with null publishedAt , but I got an error response.

Can you try published_at instead of publishedAt. Not sure if that would work but worth trying.

Thanks.
In fact, I have already tried it and failed.

Can you tell me how to customize API?

You can follow this approach to create custom api in Strapi. Use the linked example and append it with your custom logic.

Thanks, I’ll try that!

1 Like

Finally, I use this script and it works well.

'use strict';

/**
 * foo controller
 */

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::foo.foo', ({ strapi }) => ({
  async create(ctx) {
    const user = ctx.state.user; // if need
    const { body } = ctx.request;

    const newFoo = await strapi.entityService.create('api::foo.foo', {
      data: body.data
    });

    return newFoo;
  },
}));

In request, without publishedAt key, I can create a Draft Content.
This is partial example with Ruby.

options[:body] = {
  data: {
    title: 'test',
    publishedAt: DateTime.now.iso8601, # comment out to create Draft
  }
}

Great to hear that.

1 Like