# API Breaks with Multiple Lines in Body

**URL:** <https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784>\
**Category:** Platform\
**Created:** [August 19, 2019, 3:52pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784 "2019-08-19T15:52:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASilentDiplomat](https://discuss.write.as/letter_avatar_proxy/v4/letter/a/eb9ed0/32.png) [@ASilentDiplomat](https://discuss.write.as/u/ASilentDiplomat)\
**Post date:** [August 19, 2019, 3:52pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/1 "2019-08-19T15:52:58Z")

</div>

As I’ve mentioned elsewhere, I’m currently working on an add-on for [QOwnNotes](https://www.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](https://lipsum.com/)), 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?

---

<div class="post-metadata">

**Author:** ![cjeller1592](https://discuss.write.as/user_avatar/discuss.write.as/cjeller1592/32/128_2.png) [@cjeller1592](https://discuss.write.as/u/cjeller1592)\
**Post date:** [August 19, 2019, 6:50pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/2 "2019-08-19T18:50:02Z")

</div>

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:

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

```

This gets fed into a list that is converted into a string, making this [post](https://blog.cjeller.site/annotations). 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?

---

<div class="post-metadata">

**Author:** ![ASilentDiplomat](https://discuss.write.as/letter_avatar_proxy/v4/letter/a/eb9ed0/32.png) [@ASilentDiplomat](https://discuss.write.as/u/ASilentDiplomat)\
**Post date:** [August 19, 2019, 7:26pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/3 "2019-08-19T19:26:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cjeller1592](https://discuss.write.as/user_avatar/discuss.write.as/cjeller1592/32/128_2.png) [@cjeller1592](https://discuss.write.as/u/cjeller1592)\
**Post date:** [August 19, 2019, 11:01pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/4 "2019-08-19T23:01:35Z")

</div>

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

```auto
{"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.

---

<div class="post-metadata">

**Author:** ![ASilentDiplomat](https://discuss.write.as/letter_avatar_proxy/v4/letter/a/eb9ed0/32.png) [@ASilentDiplomat](https://discuss.write.as/u/ASilentDiplomat)\
**Post date:** [August 20, 2019, 10:17pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/5 "2019-08-20T22:17:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt](https://discuss.write.as/user_avatar/discuss.write.as/matt/32/2760_2.png) [@matt](https://discuss.write.as/u/matt)\
**Post date:** [August 20, 2019, 10:32pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/6 "2019-08-20T22:32:16Z")

</div>

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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![ASilentDiplomat](https://discuss.write.as/letter_avatar_proxy/v4/letter/a/eb9ed0/32.png) [@ASilentDiplomat](https://discuss.write.as/u/ASilentDiplomat)\
**Post date:** [August 20, 2019, 10:55pm UTC](https://discuss.write.as/t/api-breaks-with-multiple-lines-in-body/784/7 "2019-08-20T22:55:52Z")

</div>

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!
