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.
As of 7th February, the field is written publishedAt and not published_at as said before. So you would do like this:
const body = {
name: 'x',
description: 'y',
publishedAt: null
}
request("/endpoint", { method: âPOSTâ, body })
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?
Shekhar
December 20, 2023, 9:15am
2
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
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
}
}
Shekhar
December 21, 2023, 5:05am
4
That is what I meant above. Anyways you have the solution now.
Happy coding
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?
Shekhar
December 21, 2023, 7:06am
6
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\":{}}"
Shekhar
December 21, 2023, 9:28am
8
Shekhar:
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.
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?
Shekhar
December 21, 2023, 10:53am
10
You can follow this approach to create custom api in Strapi. Use the linked example and append it with your custom logic.
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
}
}
I am using v4.10.5
Passing "status": "Draft"
(or "status": "draft"
) did not work.
Passing "publishedAt: null
worked!
Seems like something was changed between v4.10 and v4.14.