Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Technical Support
  3. How to add line break in post body via API?

How to add line break in post body via API?

Scheduled Pinned Locked Moved Technical Support
11 Posts 3 Posters 13 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B This user is from outside of this forum
    B This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #1

    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 header content-type application/json and this data for post body

    "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
    

    I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

    baris@community.nodebb.orgB phenomlab@community.nodebb.orgP 6 Replies Last reply
    0
    • B [email protected]

      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 header content-type application/json and this data for post body

      "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
      

      I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

      baris@community.nodebb.orgB This user is from outside of this forum
      baris@community.nodebb.orgB This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #2

      Did you compare it with the request that is sent by the browser when you add new lines?

      Maybe there is something different.

      1 Reply Last reply
      0
      • B [email protected]

        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 header content-type application/json and this data for post body

        "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
        

        I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

        baris@community.nodebb.orgB This user is from outside of this forum
        baris@community.nodebb.orgB This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #3

        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"
        });
        
        B 1 Reply Last reply
        0
        • B [email protected]

          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 header content-type application/json and this data for post body

          "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
          

          I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

          phenomlab@community.nodebb.orgP This user is from outside of this forum
          phenomlab@community.nodebb.orgP This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • baris@community.nodebb.orgB [email protected]

            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"
            });
            
            B This user is from outside of this forum
            B This user is from outside of this forum
            [email protected]
            wrote on last edited by [email protected]
            #5

            baris I'm try create post via browser console, but get 403 error (with \"tid\":39, and without \"tid\":39 in body)

            4.png

            5.png

            Result:
            3.png

            1 Reply Last reply
            0
            • B [email protected]

              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 header content-type application/json and this data for post body

              "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
              

              I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

              baris@community.nodebb.orgB This user is from outside of this forum
              baris@community.nodebb.orgB This user is from outside of this forum
              [email protected]
              wrote on last edited by
              #6

              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?

              B 1 Reply Last reply
              0
              • baris@community.nodebb.orgB [email protected]

                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?

                B This user is from outside of this forum
                B This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #7

                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] - error: POST /api/v3/topics/39
                invalid csrf token
                

                Result via api.post (without error in admin/advanced/logs)

                2.png

                For the test \n\n, I'm try app.newReply and composer understand line break, but this is not post api.

                1.png

                1 Reply Last reply
                0
                • B [email protected]

                  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 header content-type application/json and this data for post body

                  "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
                  

                  I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

                  baris@community.nodebb.orgB This user is from outside of this forum
                  baris@community.nodebb.orgB This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #8

                  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#L757

                  You can add the csrf token into the headers['x-csrf-token'] or body.csrf_token in the POST request.

                  To use the api module just require it like below

                  const api = await app.require('api');
                  api.post(...);
                  
                  B 1 Reply Last reply
                  0
                  • baris@community.nodebb.orgB [email protected]

                    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#L757

                    You can add the csrf token into the headers['x-csrf-token'] or body.csrf_token in the POST request.

                    To use the api module just require it like below

                    const api = await app.require('api');
                    api.post(...);
                    
                    B This user is from outside of this forum
                    B This user is from outside of this forum
                    [email protected]
                    wrote on last edited by [email protected]
                    #9

                    baris

                    123.png

                    Ok, now this method successfully convert \n\n to line break, but how I can get this result via json api request?

                    2.png

                    1 Reply Last reply
                    0
                    • B [email protected]

                      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 header content-type application/json and this data for post body

                      "content": "user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected]"
                      

                      I get this text in post: user: [test](https://community.nodebb.org/user/test) \n\n mail: [email protected], the \n\n itn't convert to 2 line break.

                      baris@community.nodebb.orgB This user is from outside of this forum
                      baris@community.nodebb.orgB This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #10

                      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

                      B 1 Reply Last reply
                      0
                      • baris@community.nodebb.orgB [email protected]

                        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

                        B This user is from outside of this forum
                        B This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #11

                        baris

                        application/json and charset=utf-8 in content type was fix this 🙂

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups