Skip to content
  • Hjem
  • Seneste
  • Etiketter
  • Populære
  • Verden
  • Bruger
  • Grupper
Temaer
  • 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
Kollaps
FARVEL BIG TECH
  1. Forside
  2. Technical Support
  3. How to add line break in post body via API?

How to add line break in post body via API?

Planlagt Fastgjort Låst Flyttet Technical Support
11 Indlæg 3 Posters 0 Visninger
  • Ældste til nyeste
  • Nyeste til ældste
  • Most Votes
Svar
  • Svar som emne
Login for at svare
Denne tråd er blevet slettet. Kun brugere med emne behandlings privilegier kan se den.
  • B brazzerstop@community.nodebb.org

    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: 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.

    baris@community.nodebb.orgB This user is from outside of this forum
    baris@community.nodebb.orgB This user is from outside of this forum
    baris@community.nodebb.org
    wrote on sidst redigeret af
    #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 brazzerstop@community.nodebb.org

      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: 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.

      baris@community.nodebb.orgB This user is from outside of this forum
      baris@community.nodebb.orgB This user is from outside of this forum
      baris@community.nodebb.org
      wrote on sidst redigeret af
      #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 brazzerstop@community.nodebb.org

        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: 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.

        phenomlab@community.nodebb.orgP This user is from outside of this forum
        phenomlab@community.nodebb.orgP This user is from outside of this forum
        phenomlab@community.nodebb.org
        wrote on sidst redigeret af
        #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 baris@community.nodebb.org

          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
          brazzerstop@community.nodebb.org
          wrote on sidst redigeret af
          #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 brazzerstop@community.nodebb.org

            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: 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.

            baris@community.nodebb.orgB This user is from outside of this forum
            baris@community.nodebb.orgB This user is from outside of this forum
            baris@community.nodebb.org
            wrote on sidst redigeret af
            #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 baris@community.nodebb.org

              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
              brazzerstop@community.nodebb.org
              wrote on sidst redigeret af
              #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 brazzerstop@community.nodebb.org

                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: 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.

                baris@community.nodebb.orgB This user is from outside of this forum
                baris@community.nodebb.orgB This user is from outside of this forum
                baris@community.nodebb.org
                wrote on sidst redigeret af
                #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 baris@community.nodebb.org

                  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
                  brazzerstop@community.nodebb.org
                  wrote on sidst redigeret af brazzerstop@community.nodebb.org
                  #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?

                  1.png

                  2.png

                  1 Reply Last reply
                  0
                  • B brazzerstop@community.nodebb.org

                    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: 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.

                    baris@community.nodebb.orgB This user is from outside of this forum
                    baris@community.nodebb.orgB This user is from outside of this forum
                    baris@community.nodebb.org
                    wrote on sidst redigeret af
                    #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 baris@community.nodebb.org

                      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
                      brazzerstop@community.nodebb.org
                      wrote on sidst redigeret af
                      #11

                      baris

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

                      1 Reply Last reply
                      0
                      Svar
                      • Svar som emne
                      Login for at svare
                      • Ældste til nyeste
                      • Nyeste til ældste
                      • Most Votes


                      • Log ind

                      • Har du ikke en konto? Tilmeld

                      • Login or register to search.
                      Powered by NodeBB Contributors
                      Graciously hosted by data.coop
                      • First post
                        Last post
                      0
                      • Hjem
                      • Seneste
                      • Etiketter
                      • Populære
                      • Verden
                      • Bruger
                      • Grupper