Posting new topic via the API not working

AI-generated summary

In a Support thread titled “Posting new topic via the API not working,” lkramer initially struggled to create a test topic via Discourse’s API, receiving a list of latest posts instead of a successful post. They shared their malformed GET request URL with unencoded spaces in title and raw fields, and incorrect HTTP method usage. blake advised checking the request method (should be POST), recommended tools like Postman, and flagged the space issue in parameters. Following this, lkramer resolved the issue by: 1) using http_build_query() to properly encode parameters, and 2) ensuring the title and message met Discourse’s minimum length requirements (e.g., “test” alone fails). They shared a working PHP cURL example that POSTs to posts.json with proper authentication, title, raw content, and category — intended to let WordPress users auto-post introductions or questions to their Discourse forum. The key takeaways are: always use POST, encode parameters, and meet Discourse’s content length rules.

Hello. I’ve read the documentation and searched for examples here but I can’t seem to get a simple test topic working. Here’s what I’m doing:

https://my-forum-url/posts.json?title=this is a test topic via the api&category=5&raw=this is the test message part also via the api&api_key=my-forum-key&api_username=system

Every time I execute it, it returns a list of the latest posts.

Any ideas? Thanks!

Could you give us a little bit more detail on how you are using the api? Are you trying this with curl? are you using postman (if not, I would highly recommend it)? Are you making a POST request?. Are you getting any errors back? My first thought is that you have spaces in your raw and title params and this probably won’t work if you are using curl.

Thanks for the reply! I ended up scrutinizing my php code a bit more and got it working! First mistake was not wrapping the query args in http_build_query (thanks to your clue about spaces). Second mistake was that you have to adhere to Discourse’s rules about the title and message not being too brief (e.g, “test” / “test” doesn’t post).

Here’s the basic working proof of concept code in case anyone finds it useful. We’re planning to use it to allow people to introduce themselves or ask a question via our main site (wordpress-powered).

// change this to your forum's info
$api_key="real-api-key-here";
$api_user="system";
$url_base="https://real-forum-url-here/";
$api_auth="api_key=" . $api_key . "&api_username=" . $api_user;

// title and message
$title = $_GET['dt'];
$message = $_GET['dm'];

$ch = curl_init();	

$query_args = array(
'title' => $title,
'raw' => $message,
'category' => 7,
);

$url=$url_base . "posts.json?" . $api_auth;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query_args) );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$response = curl_exec($ch);

echo '<br>response-start:';
print_r($response);
echo ':response-end';