How to add line break in post body via API?
-
Hello!
I was create post request via API v3 to create a post (docs)
This is works, but I can't add a new line in content, this value not work
\n
. I'm use this format in headercontent-type application/json
and this data for post body"content": "user: @test \n\n mail: [email protected]"
I get this text in post:
user: @test \n\n mail: [email protected]
, the\n\n
itn't convert to 2 line break. -
When I copy the above posts request from my dev tools it shows the below so maybe try escaping the
\n\n
like\\n\\n
fetch("https://community.nodebb.org/api/v3/topics/17371", { "headers": { ... }, "body": "{\"tid\":17371,\"content\":\"Did you compare it with the request that is sent by the browser when you add new lines?\\n\\nMaybe there is something different.\"}", "method": "POST" });
-
@brazzerstop if it's any consolation, I get the same issue with similar code I developed that creates posts using the API.
I couldn't get new lines working no matter what I tried.
-
@baris when I try this:
fetch("https://example.com/api/v3/topics/39", { "headers": { "Authorization": "Bearer 123", }, "body": "{\"content\":\"Did you compare it with the request that is sent by the browser when you add new lines?\\n\\nMaybe there is something different.\"}", "method": "POST" });
I have this error in
admin/advanced/logs
:2023-07-02T12:48:39.085Z [4567/641] - [31merror[39m: POST /api/v3/topics/39 invalid csrf token
Result via
api.post
(without error in admin/advanced/logs)For the test
\n\n
, I'm tryapp.newReply
and composer understand line break, but this is not post api. -
Well the composer uses the api it makes a POST request to the
/topics/:tid
end point. https://github.com/NodeBB/nodebb-plugin-composer-default/blob/master/static/lib/composer.js#L757You can add the csrf token into the
headers['x-csrf-token']
orbody.csrf_token
in the POST request.To use the api module just require it like below
const api = await app.require('api'); api.post(...);
-