Posting new topic via the API not working

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.

2 Likes

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';
3 Likes