API Breaks with Multiple Lines in Body

As I’ve mentioned elsewhere, I’m currently working on an add-on for QOwnNotes that will allow posting to write.as directly from within that program. QOwnNotes scripts are done in QML, which is basically JavaScript plus some Qt-specific stuff.

Anyway, I’ve got a basic posting script done, but it breaks anytime you have multiple paragraphs in the body text. I’ve tried a note that has multiple paragraphs (thank you, Lorem Ipsum Generator), but this always returns a 400 error from write.as’s servers. I believe QOwnNotes uses \n to represent new lines. For one, Qt apps in general tend to be consistent with web (rather than OS) text formatting. in addition, I have a method as part of my script that tries to find the title of the post, and that involves splitting the post with \n as the separator (and this works as expected).

I’ve also just added \n to an arbitrary string and tried to send that as a body, but I again get a 400 return from the server.

So, is this a bug, or is there some way I need to format the string I’m sending to write.as in order for it to be treated correctly?

Hey @ASilentDiplomat,

I use multiple lines a ton when posting to the API, especially with \n formatting. It doesn’t break for me. Here is an example of string formatting in a Python app I use:

thing = '[{}]({})\n\n'.format(title, link)

This gets fed into a list that is converted into a string, making this post. Don’t know if that helps any, but just an indication that multiple lines should be possible.

Does the response body in the 400 specify anything in particular? Do you have the string in question that we could take a look at?

Weird. Nothing exotic in what I’m doing that I can tell.

However, it does seem to be specific to QOwnNotes. If I do a regular curl request with this as the data:

'{"body": "Test post.\n\n More test.", "title": "Test post"}'

it works fine. But as soon as I send that (literally copied and pasted into the code) via an XMLHttpRequest in the script, it breaks. Doing the same string without the \n\n works fine.

I’m going to reach out to the dev of QOwnNotes and see if he has any ideas.

Ah okay, so XMLHTTPRequest does work without \n in the string. Did you try using \\n instead? That data becomes:

{"body": "Test post.\\n\\n More test.", "title": "Test post"}

I have seen this work before on some similar problems. If that doesn’t work, give \r\n a shot.

So \\n works if it’s put into the XMLHttpRequest manually, i.e. by using the test string you have.

But if I have a note within QOwnNotes with multiple paragraphs, it still returns a 400 on write.as’s side. This is true even if I run a replace on the body text and replace \n with \\n. I’ve also tried replacing \n with \r\n, but the results are the same.

Just to see, I also tried replacing \r\n and \0, but in each case I still get 400.

Pinging @matt too to see if he has any ideas.

Hmm, really not sure off the top of my head. I am seeing several JSON parsing errors on the server with this message:

invalid character 'O' looking for beginning of value

Is there any way you can share the exact JSON payload being sent? Maybe QOwnNotes is injecting extra characters somewhere?

Thanks for the reply matt, and that’s probably me.

A ha! That was actually a big help. “O” happened to be the first character of my dummy text, which was the clue. It turns out I’d mis-formed the string that was to be uploaded (was missing an internal "). With that, combined with the previous suggestion that I use \\n rather than \n, and things are working!

1 Like