API vs. Form: Content Mismatch When Edited


I just noticed that when I post HTML content using the API posts.json and then I edit the post, I see the HTML in the content of the post.

How the post displays:

When I click on edit:

API request:

When I post directly via the form, no HTML is showing up in the edit (the display is the same):

How can I avoid having HTML in the content when the post is edited?

Thank you :slight_smile:

1 Like

Hi Luicid :wave:

The raw content, which is displayed in the composer, accept an array of HTML tags, thus it will display them in plain text.

If you want to add a line break without using a <br> tag, use \n instead. :slight_smile:

First line\nNew line

If you want to have two paragraphs, you need to add an additional line break:

First line\n\nNew line

Editing these messages won’t show any <br> tag.

Note: paragraphs are created automatically depending on the context when the post is processed, so there’s no need to add these tags to your query raw input.


Hi again Canapin :slight_smile:

Ok, I see. So the only option to have a clean display when users edit their migrated HTML posts via the API is to replace all <p> and <br> tags with \n if I understand correctly?

Thank you


If you try to sanitize some imported HTML content, I suppose you can replace paragraphs with line breaks as well, so you’ll be sure there are line breaks, and the post will be processed properly.

<p>some text</p><p>some other text</p>

would become:

\nsome text\n\nsome other text\n

And will render as:

some text

some text


Ok, thank you :slight_smile:
Last question:
Is there any way you can tell me how to add a mention, like this one: @Canapin, in a post via the API? I tried using @username in the raw field, but it doesn’t work.

Be sure you send a JSON payload.

    "raw": "Test mention: @canapin",
    "topic_id": 86,
    "reply_to_post_number": 3

Result is a working mention:


If you’re importing a bunch of data, you should use an import script. Many of them have examples of stripping html and handling quotes and mentions.

If you want to continually add data to am existing site, use the API.

1 Like

Ah, I see my mistake! I was testing with a lowercase username instead of using the exact username, as the mention is case-sensitive. My apologies.

Thank you for taking the time to help me! :wink:

Have a nice day !

Honestly, I created a POC with an import script in Ruby, but since I need to synchronize data for several days between an old solution and Discourse, I prefer running a simple Python script. It’s much faster to set up and maintain. Additionally, it’s consistent, as I scrape the old data with Python. The previous provider won’t grant us access to the database, so I scraped all the content into a database.

But of course it’s a better solution if it’s a simple migration :slight_smile:

Mentions should work even if we mess with the capitalization:


The screenshot in your first post uses a form-data. You should use a raw field with JSON formatting. :slight_smile:


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.