Getting 500 Internal Server Error while using Post API


#1

I am trying to import Jobs from a public job board as separate posts into the database. Basically it already works, but as soon as I want to add the job description (HTML) into the MobileDoc part the API blocks me and gives me a 500 and I am not sure why.

Here is the JS I use (without my login credentials :slight_smile: …)

const GREENHOUSE_URL = 'https://api.greenhouse.io/v1/boards/kilabs';
const GHOST_URL = 'ghost/api/v0.1';
// const GHOST_URL = ghost.url.api('posts').replace(/\/?\?.*$/, '');
const GHOST_CLIENT_ID = 'ghost-frontend';
const GHOST_CLIENT_SECRET = '85f5bc2d8daa';

const uuid = () => {
	const s4 = () =>
		Math.floor((1 + Math.random()) * 0x10000)
			.toString(16)
			.substring(1);

	return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
};

const fetchAllJobs = () =>
	fetch(`${GREENHOUSE_URL}/jobs`)
		.then(x => x.json())
		.then(x => x.jobs);

const fetchJob = id =>
	fetch(`${GREENHOUSE_URL}/jobs/${id}`).then(x => x.json());

const authenticateGhost = (username, password) =>
	fetch(`${GHOST_URL}/authentication/token`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			username,
			password,
			grant_type: 'password',
			client_id: GHOST_CLIENT_ID,
			client_secret: GHOST_CLIENT_SECRET,
		}),
	})
		.then(x => x.json())
		.then(x => x.access_token);

const postJobs = (token, posts) =>
	fetch(`${GHOST_URL}/posts`, {
		method: 'POST',
		headers: {
			Authorization: `Bearer ${token}`,
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			posts,
		}),
	});

const transferJobs = async () => {
	const jobsList = await fetchAllJobs();
	const jobs = await Promise.all(jobsList.map(x => fetchJob(x.id)));
	const ghostToken = await authenticateGhost(
		'xxx',
		'xxx',
	);

	const makeMobileDoc = (content) => {
		return {
			version: "0.3.1",
			markups: [],
			atoms: [],
			cards: [
				["card-markdown", {
					"cardName": "card-markdown",
					"markdown": content
					}
				],
			],
			sections: [[10,0]]
		}
	}

	const posts = jobs.map(x =>
		Object.assign(
			{},
			{
				uuid: uuid(),
				title: x.title,
				author: "1",
				mobiledoc: makeMobileDoc(x.content),
				status: "published",
				published_by: "None",
				page: false,
				featured: false,
			},
		),
	);
	postJobs(ghostToken, posts);
};

transferJobs();

And I always end up getting this with MobileDoc:

POST http://localhost:2368/ghost/api/v0.1/posts 500 (Internal Server Error)

Can anybody give me hint why this is happening?


#2

Can you please share the error log / error stack? Thanks!


#3

Sure, here you are. The latest from the Ghost log:

    [2018-05-29 17:08:17] INFO "GET /assets/js/jquery.fitvids.js?v=bedc2eaa96" 304 16ms
[2018-05-29 17:08:17] INFO "GET /assets/built/animate-min.css?v=bedc2eaa96" 304 58ms
[2018-05-29 17:08:17] INFO "GET /assets/built/style.css?v=bedc2eaa96" 304 65ms
[2018-05-29 17:08:17] INFO "GET /assets/js/infinitescroll.js?v=bedc2eaa96" 304 40ms
[2018-05-29 17:08:18] INFO "GET /assets/built/global.css" 304 2ms
[2018-05-29 17:08:18] INFO "GET /assets/map.svg" 304 3ms
[2018-05-29 17:08:18] INFO "GET /assets/erik2.jpg" 304 6ms
[2018-05-29 17:08:18] INFO "GET /assets/bg17.jpg" 304 47ms
[2018-05-29 17:08:18] INFO "GET /assets/bg1.jpg" 304 1ms
[2018-05-29 17:08:18] INFO "GET /assets/bg11.jpg" 304 1ms
[2018-05-29 17:08:18] INFO "GET /assets/built/global.css.map" 304 2ms
[2018-05-29 17:08:18] INFO "GET /assets/bg10.jpg" 304 4ms
[2018-05-29 17:08:18] INFO "GET /assets/bg12.jpg" 304 4ms
[2018-05-29 17:08:18] INFO "GET /assets/fonts/OpenSans-Regular.ttf" 304 1ms
[2018-05-29 17:08:18] INFO "GET /assets/fonts/OpenSans-Bold.ttf" 304 3ms
[2018-05-29 17:08:18] INFO "GET /assets/fonts/OpenSans-SemiBold.ttf" 304 8ms
[2018-05-29 17:08:18] INFO "GET /assets/bg16.jpg" 304 1ms
[2018-05-29 17:08:18] INFO "GET /assets/fonts/OpenSans-Light.ttf" 304 4ms
[2018-05-29 17:08:24] INFO "POST /ghost/api/v0.1/authentication/token" 200 160ms
[2018-05-29 17:08:24] ERROR "POST /ghost/api/v0.1/posts" 500 48ms

NAME: InternalServerError
MESSAGE: Unexpected token o in JSON at position 1

level: normal

InternalServerError: Unexpected token o in JSON at position 1
    at new GhostError (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/lib/common/errors.js:9:26)
    at prepareError (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/web/middleware/error-handler.js:39:19)
    at Layer.handle_error (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:315:13)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:315:13)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:275:10)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:635:15
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:260:14)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:127:14)
    at onAPIError (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/api/index.js:290:13)

SyntaxError: Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Child.onSaving (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/post.js:189:30)
    at Child.onSaving (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/relations/authors.js:151:35)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bookshelf/lib/base/events.js:176:25
    at tryCatcher (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/util.js:16:23)
    at Object.gotValue (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/reduce.js:155:18)
    at Object.gotAccum (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/reduce.js:144:25)
    at Object.tryCatcher (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bluebird/js/release/async.js:17:14)
From previous event:
    at Child.triggerThen (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bookshelf/lib/base/events.js:175:32)
    at Child.<anonymous> (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bookshelf/lib/model.js:1110:19)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
From previous event:
    at Child.<anonymous> (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bookshelf/lib/model.js:1010:8)
From previous event:
    at Child.save (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/plugins/collision.js:88:47)
    at Function.add (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/base/index.js:787:22)
    at addPost (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/post.js:689:45)
    at ghostBookshelf.transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/post.js:702:24)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/plugins/transaction-events.js:23:20
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/knex/lib/transaction.js:81:20
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
From previous event:
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/knex/lib/transaction.js:75:10
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
From previous event:
    at new Transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/knex/lib/transaction.js:68:41)
    at Client_SQLite3.transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/knex/lib/client.js:159:12)
    at Object.transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/knex/lib/util/make-knex.js:75:21)
    at Object.transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/bookshelf/lib/bookshelf.js:249:36)
    at Object.bookshelf.transaction (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/plugins/transaction-events.js:9:37)
    at Function.add (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/models/post.js:699:35)
    at modelQuery (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/api/posts.js:197:32)
    at runTask (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/lib/promise/pipeline.js:16:24)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/lib/promise/pipeline.js:26:20
From previous event:
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/lib/promise/pipeline.js:25:24
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
From previous event:
    at pipeline (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/lib/promise/pipeline.js:23:30)
    at add (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/api/posts.js:219:16)
    at apiHandler (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/api/index.js:268:16)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at uncapitalise (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/web/middleware/uncapitalise.js:53:5)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at slashes (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/connect-slashes/lib/connect-slashes.js:81:9)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at urlRedirects (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/web/middleware/url-redirects.js:111:5)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at cors (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/cors/lib/index.js:188:7)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/cors/lib/index.js:224:17
    at originCallback (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/cors/lib/index.js:214:15)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/cors/lib/index.js:219:13
    at handleCORS (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/web/middleware/api/cors.js:82:16)
From previous event:
    at Child.then (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/services/auth/auth-strategies.js:46:30)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)
From previous event:
    at Strategy.bearerStrategy [as _verify] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/services/auth/auth-strategies.js:41:14)
    at Strategy.authenticate (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/passport-http-bearer/lib/strategy.js:132:10)
    at attempt (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/passport/lib/middleware/authenticate.js:361:16)
    at authenticate (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/passport/lib/middleware/authenticate.js:362:7)
    at authenticateUser (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/services/auth/authenticate.js:100:10)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at authenticateClient (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/core/server/services/auth/authenticate.js:28:20)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at /Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/s.read/Documents/GitHub/techhub/versions/1.22.8/node_modules/express/lib/router/index.js:317:13)

#4

@Starburst1977 I think you’ll need to convert your mobiledoc object to a JSON string:

mobiledoc: JSON.stringify(makeMobileDoc(x.content))

#5

Perfect, thanks. How could I miss that. Shame on me :slight_smile:


#6

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.