I’m trying to push post using Ghost API, using Pipedream. Below you may find API key, but I deleted those. If you require more information, let me know.
const axios = require('axios');
const apiKey = 'key_here'; // Replace with your Ghost API key
const ghostApiUrl = 'https://website/ghost/api/v3/admin/posts'; // Replace with your Ghost blog URL
async function createDraftPost(title, content, feature_image, meta_title, meta_description, excerpt) {
const postData = {
posts: [
{
title,
html: content,
feature_image,
meta_title,
meta_description,
custom_excerpt: excerpt,
status: 'draft',
},
],
};
const response = await axios.post(ghostApiUrl, postData, {
headers: {
Authorization: `Ghost ${apiKey}`,
'Content-Type': 'application/json',
},
});
return response.data;
}
(async () => {
const title = 'Your post title';
const content = 'Your post content';
const feature_image = 'Your post feature image URL';
const meta_title = 'Your post meta title';
const meta_description = 'Your post meta description';
const excerpt = 'Your post excerpt';
try {
const result = await createDraftPost(title, content, feature_image, meta_title, meta_description, excerpt);
console.log('Post created:', result.posts[0]);
} catch (error) {
console.error('Error creating post:', error);
}
})();
THE ERROR
Error creating post: AxiosError: Request failed with status code 400
at settle (/pipedream/dist/code/425107edd508ba3469ef2101b2badf6dc928fd6fbba8c71c5b8531f6769a4754/node_modules/.pnpm/axios@1.3.5/node_modules/axios/dist/node/axios.cjs:1898:12)
at IncomingMessage.handleStreamEnd (/pipedream/dist/code/425107edd508ba3469ef2101b2badf6dc928fd6fbba8c71c5b8531f6769a4754/node_modules/.pnpm/axios@1.3.5/node_modules/axios/dist/node/axios.cjs:2950:11)
at IncomingMessage.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1333:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
code: 'ERR_BAD_REQUEST',
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [ 'xhr', 'http' ],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
env: { FormData: [Function], Blob: null },
validateStatus: [Function: validateStatus],
headers: AxiosHeaders {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
Authorization: 'Ghost *redacted*',
'User-Agent': 'axios/1.3.5',
'Content-Length': '250',
'Accept-Encoding': 'gzip, compress, deflate, br'
},
method: 'post',
url: 'https://healthvery.com/ghost/api/v3/admin/posts',
data: '{"posts":[{"title":"Your post title","html":"Your post content","feature_image":"Your post feature image URL","meta_title":"Your post meta title","meta_description":"Your post meta description","custom_excerpt":"Your post excerpt","status":"draft"}]}'
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype] {
abort: [Function (anonymous)],
aborted: [Function (anonymous)],
connect: [Function (anonymous)],
error: [Function (anonymous)],
socket: [Function (anonymous)],
timeout: [Function (anonymous)],
prefinish: [Function: requestOnPrefinish]
},
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
secureConnecting: false,
_SNICallback: null,
servername: 'healthvery.com',
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object: null prototype],
_eventsCount: 10,
connecting: false,
_hadError: false,
_parent: null,
_host: 'healthvery.com',
_readableState: [ReadableState],
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular *1],
[Symbol(res)]: [TLSWrap],
[Symbol(verified)]: true,
[Symbol(pendingSession)]: null,
[Symbol(async_id_symbol)]: 17,
[Symbol(kHandle)]: [TLSWrap],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object],
[Symbol(RequestTimeout)]: undefined
},
_header: 'POST /ghost/api/v3/admin/posts HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'Authorization: Ghost *redacted*\r\n' +
'User-Agent: axios/1.3.5\r\n' +
'Content-Length: 250\r\n' +
'Accept-Encoding: gzip, compress, deflate, br\r\n' +
'Host: healthvery.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: Agent {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: [Object],
[Symbol(kCapture)]: false
},
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/ghost/api/v3/admin/posts',
_ended: true,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
socket: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 400,
statusMessage: 'Bad Request',
client: [TLSSocket],
_consuming: false,
_dumped: false,
req: [Circular *1],
responseUrl: 'https://healthvery.com/ghost/api/v3/admin/posts',
redirects: [],
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
},
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'healthvery.com',
protocol: 'https:',
_redirectable: Writable {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
_options: [Object],
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 250,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: [Circular *1],
_currentUrl: 'https://healthvery.com/ghost/api/v3/admin/posts',
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'content-type': [Array],
authorization: [Array],
'user-agent': [Array],
'content-length': [Array],
'accept-encoding': [Array],
host: [Array]
}
},
response: {
status: 400,
statusText: 'Bad Request',
headers: AxiosHeaders {
server: 'nginx/1.18.0 (Ubuntu)',
date: 'Fri, 07 Apr 2023 12:40:36 GMT',
'content-type': 'application/json; charset=utf-8',
'content-length': '210',
connection: 'close',
'x-powered-by': 'Express',
deprecation: 'version="v3"',
link: '<https://healthvery.com/ghost/api/admin/posts>; rel="latest-version"',
'content-version': 'v5.33',
vary: 'Accept-Version, Accept-Encoding',
'cache-control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0',
etag: 'W/"d2-Oe3I4/6PBAI//4tvwnravzmXmN4"'
},
config: {
transitional: [Object],
adapter: [Array],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
env: [Object],
validateStatus: [Function: validateStatus],
headers: [AxiosHeaders],
method: 'post',
url: 'https://healthvery.com/ghost/api/v3/admin/posts',
data: '{"posts":[{"title":"Your post title","html":"Your post content","feature_image":"Your post feature image URL","meta_title":"Your post meta title","meta_description":"Your post meta description","custom_excerpt":"Your post excerpt","status":"draft"}]}'
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [TLSSocket],
_header: 'POST /ghost/api/v3/admin/posts HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: application/json\r\n' +
'Authorization: Ghost *redacted*\r\n' +
'User-Agent: axios/1.3.5\r\n' +
'Content-Length: 250\r\n' +
'Accept-Encoding: gzip, compress, deflate, br\r\n' +
'Host: healthvery.com\r\n' +
'Connection: close\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/ghost/api/v3/admin/posts',
_ended: true,
res: [IncomingMessage],
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'healthvery.com',
protocol: 'https:',
_redirectable: [Writable],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
data: { errors: [Array] }
}
}