Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid JSON response body when starting up #32

Open
sylverstream opened this issue Jul 2, 2023 · 7 comments
Open

Invalid JSON response body when starting up #32

sylverstream opened this issue Jul 2, 2023 · 7 comments

Comments

@sylverstream
Copy link

I've got a bot for lemmy.world and it has stopped working since they've upgraded to 0.18.1-rc.4
It fails at logging in:

Starting bot
Initializing DB
app listening at http://localhost:1234
logging in

D:\projects\lemmybot\node_modules\node-fetch\lib\index.js:273
                                return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
                                                           ^
FetchError: invalid json response body at https://lemmy.world/api/v3/user/login reason: Unexpected token J in JSON at position 0
    at D:\projects\lemmybot\node_modules\node-fetch\lib\index.js:273:32
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  type: 'invalid-json'
}

I've double checked, the account credentials I'm passing in are valid.

@mtthwnestor
Copy link

My personal Lemmy instance is running 0.18.0, and lemmy-bot has no problem logging in. However, I'm seeing the FetchError: invalid json response body error when trying to create a post. Is this possibly related, or am I just doing something wrong?

Starting bot
Initializing DB
logging in
logged in
Marking account as bot account
user_already_exists
Creating post

/app/node_modules/node-fetch/lib/index.js:273
				return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
				                           ^
FetchError: invalid json response body at https://lemmy.mydomain.net/api/v3/post reason: Unexpected token 'J', "Json deser"... is not valid JSON
    at /app/node_modules/node-fetch/lib/index.js:273:32
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: 'invalid-json'
}

@SleeplessOne1917
Copy link
Owner

I'm trying to figure out if this is a problem with the bot library, the js client library, or the server-side of the API. I'm seen similar errors of invalid JSON bodies pop up in error toasts on the UI project sometimes.

@mtthwnestor
Copy link

The error messages seem to be coming this way because Lemmy API returns plain text instead of JSON. This issue just got closed, so hopefully, the errors will be handled in a more useful way in the next release: LemmyNet/lemmy#3366

However, I still don't know what is triggering the error for me in the first place. I am not that good with Typescript yet, so I am probably doing something wrong, but getting a more reasonable error message should help me as I try to figure it out.

@nebbishOne
Copy link

nebbishOne commented Jul 16, 2023

I am getting this error as well. I have lemmy-bot 0.4.5, node 18.16.1, npm 9.5.1.

I am just learning node, etc... really. I just created a new project, a new index.js in that folder and then copied in the Congratulator bot example, since that's close to what I want to do. I did 'npm init' and 'npm install'. I edited the credentials, and the comment text in the JS file. I run it as 'node index.js' from the terminal and getting the Invalid JSON message above.

Starting bot
Creating database file
Initializing DB
logging in
logged in
Marking account as bot account
user_already_exists

/app/node_modules/node-fetch/lib/index.js:273
				return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));

Node.js v18.16.1

Is there anything I can edit on my PC to get this working? Or do we need to wait for the next Lemmy release? Thanks for any pointers.

EDIT TO ADD: Thanks to sylverstream, I added the same console.log() and got this:

Json deserialize error: missing field `post_id` at line 1 column 198

EDIT TO ADD AGAIN: I found a defect in my bot script. It was passing in "postId" but should have passed "post_id". With that fixed, my bot is working! @sylverstream - check your code for a similar mistake, maybe. I think the bot library itself is OK, in other words.

@sylverstream
Copy link
Author

sylverstream commented Jul 17, 2023

I've also got the same error. In my case, the logging in fails. I've added a console.log in node_modules/node-fetch/lib/index.js to see the Lemmy response.
I've added this code at line 273 (in the catch block):
console.log(buffer.toString());

In my case the error returned by Lemmy.world is: Json deserialize error: missing field username_or_email at line 1 column 1

@SleeplessOne1917 hopefully that helps?

@etymotic
Copy link

The error messages seem to be coming this way because Lemmy API returns plain text instead of JSON. This issue just got closed, so hopefully, the errors will be handled in a more useful way in the next release: LemmyNet/lemmy#3366

However, I still don't know what is triggering the error for me in the first place. I am not that good with Typescript yet, so I am probably doing something wrong, but getting a more reasonable error message should help me as I try to figure it out.

I wonder, are we still waiting on an update to Lemmy before this is addressed?

For me this only happens when trying to pin a post using Lemmy-Frank-Bot.

@SleeplessOne1917
Copy link
Owner

I wonder, are we still waiting on an update to Lemmy before this is addressed?

Lemmy version 0.19.0 is going to release (hopefully) this week, so hopefully this will allow us to check whether this is an issue with the API or the bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants