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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break. -
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break.Did you compare it with the request that is sent by the browser when you add new lines?
Maybe there is something different.
-
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, 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" });
-
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break.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.
-
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" });
baris I’m try create post via browser console, but get 403 error (with
\"tid\":39
, and without\"tid\":39
in body)Result:
-
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break.What do you see when you log data.content here? Compare it to a regular post made with the composer. Did you try to make the post with
api.post('/topics/123', {tid: tid, content: '....'});
and see if that works? -
What do you see when you log data.content here? Compare it to a regular post made with the composer. Did you try to make the post with
api.post('/topics/123', {tid: tid, content: '....'});
and see if that works?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. -
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break.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(...);
-
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(...);
Ok, now this method successfully convert
\n\n
to line break, but how I can get this result via json api request? -
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](https://community.nodebb.org/user/test) \n\n mail: test@test.com"
I get this text in post:
user: [test](https://community.nodebb.org/user/test) \n\n mail: test@test.com
, the\n\n
itn’t convert to 2 line break.Not sure, maybe take a look at the data sent by that tool you are using and see if it matches the network request for
api.post
-
Not sure, maybe take a look at the data sent by that tool you are using and see if it matches the network request for
api.post
application/json and charset=utf-8 in content type was fix this