• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
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
v3.5.2 Latest
Buy Hosting

How to add line break in post body via API?

Scheduled Pinned Locked Moved Solved Technical Support
11 Posts 3 Posters 201 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.
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    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
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    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
    1
  • phenomlabP Offline
    phenomlabP Offline
    phenomlab
    replied to brazzerstop 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
    1
  • B Offline
    B Offline
    brazzerstop
    replied to <baris> on last edited by
    #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

    Request:
    2.png

    Result:
    3.png

    1 Reply Last reply
    0
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    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
  • B Offline
    B Offline
    brazzerstop
    replied to <baris> 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
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    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
    1
  • B Offline
    B Offline
    brazzerstop
    replied to <baris> on last edited by
    #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
  • barisB Offline
    barisB Offline
    <baris> NodeBB
    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
    1
  • B Offline
    B Offline
    brazzerstop
    replied to <baris> on last edited by
    #11

    @baris

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

    1 Reply Last reply
    2
  • B brazzerstop has marked this topic as solved on

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development