Failed to post a topic

tried to post a topic with the API, it failed with this error msg:
{‘action’: ‘create_post’, ‘errors’: [‘Body is too short (minimum is 1 character)’, ‘Body seems unclear, is it a complete sentence?’]}

  • the message to post is around 25K, a list of news
    can post it manually without error.
    but when post it with API, it fails
    the default post size is 35K(?), already increase it (in settings) to allow this longer message.
  • if post a short sentence with the code below, it works fine.

changed body min entropy to 0, it did not help either.

searched the forum, not found a solution,
any idea/suggestions, thanks…

here is the python code

import requests

# Authentication headers
headers = {
    "Api-Key": '***',
    "Api-Username": 'newsbot',
}

lines = "some news, around 25K"

# Topic data
topic_data = {
    "title":"latest news",
    "raw": lines,
    "category": 28,  
}

# Send a POST request to create the topic
response = requests.post('https://www.mydomain.com/posts.json', json=topic_data, headers=headers)

# Check if the request was successful
if response.status_code == 200:
    print("Topic created successfully!")
    topic_id = response.json().get("id")
    print(f"Topic ID: {topic_id}")
else:
    print(response.json())
    print(f"Failed to create the topic. Status code: {response.status_code}")

btw, the content to post is a list of news like this

[grid]
![](https://static01.nyt.com/images/2023/10/16/multimedia/animage.jpg)
![](https://content.api.news/v3/images/bin/image.jpg)
[/grid]

<details><summary><b>news title</b></summary>
news conentnews conentnews conentnews conentnews 
conent news conentnews conentnews conentnews conentnews 
conentnews conentnews conentnews conentnews conentnews 
conentnews conentnews conentnews conentnews conent
about this long for one news

[The New source 2023-10-16T21:16:45Z](https://www.newssource.com/live/anews)

Try adding "skip_validations": true to the topic_data. (There are cases where true needs to be enclosed in quotation marks, but I don’t think that’s the case with Python.)

The skip_validations parameter will get Discourse to skip performing the validations against site settings like min topic title length and body min entropy.

3 Likes

thanks @simon

tried

"skip_validations": True

it suppressed the error, created a topic, but it is empty, hmm…
suspect maybe the html syntax in my post content needs to be coded ?

1 Like

Random guess… Maybe set the Content-Type as application/json?

tried in header or body (topic_data) , it still create a new empty topic post.

I tried your python script (without specifying the content-type) on my instance and it worked perfectly:

image

1 Like

I have to ask it, but does your code work with a short text? Did you check that you actually pass non-empty content in the API?

1 Like

yes, it works for short content, but not the long one (attached), 29848 chars, not due to the length,
suspected maybe some special character ?

thanks for your help !
test.zip (12.1 KB)

1 Like

thanks @Arkshine
yes, mentioned in the posts above…

1 Like

I tried with your text and it works:

Did you enclose your string with triple quotes (either ''' or """) to avoid escaping issues?

How do you load your post’s content in your python script (I suppose you take your content from an external source)?

1 Like

Same on my side; I have no issue with your content. I guess there is something wrong with how you retrieve/load the data. :thinking:

1 Like

hmm… thanks for trying… interesting…
I loaded the news content from a file like this…

with open(sys.argv[1]) as f:
    lines = [line for line in f]
 
print(lines)
print(len(lines))

it got the file content correctly, hmm…

Did you try passing encoding="utf8" in open()? That might help.

2 Likes

yes, something not right with loading the file content…
thanks… false alarm… :slight_smile:

1 Like

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