Jump to content

Featured Replies

Posted

Hello,

I want to add posts on the Invision Community 4.7.19 with API

Here’s my function:

def post_forum_reply(topic_id, reply_text):
    logging.info(f"Posting reply to topic ID: {topic_id}")
    url = f"{FORUM_API_URL}/forums/posts"
    headers = {
        "Content-Type": "application/json",
        "User-Agent": "MyUserAgent/1.0"
    }
    payload = {
        "topic": int(topic_id),  # Ensure topic ID is an integer
        "author": int(USER_MENTION_ID),  # Ensure this is an integer
        "post": f"<p>{reply_text}</p>",  # Your post content
    }
    logging.debug(f"POST URL: {url}")
    logging.debug(f"Headers: {headers}")
    logging.debug(f"Payload: {payload}")

    response = requests.post(
        url,
        auth=HTTPBasicAuth(FORUM_API_KEY, ''),  # Using HTTP Basic Authentication
        headers=headers,
        json=payload
    )
    logging.debug(f"Response status code: {response.status_code}")
    logging.debug(f"Response content: {response.content}")
    response.raise_for_status()
    return response.json()

It throws an error indicating that the topic doesnt exist, but it does though:

2025-01-26 20:20:00,329 - INFO - Posting reply to topic ID: 80687

2025-01-26 20:20:00,329 - DEBUG - POST URL: https://forum.url.com/api/forums/posts

2025-01-26 20:20:00,329 - DEBUG - Headers: {'Content-Type': 'application/json', 'User-Agent': 'MyUserAgent/1.0'}

2025-01-26 20:20:00,329 - DEBUG - Payload: {'topic': 80687, 'author': 23055, 'post': '<p>post content</p>'}

2025-01-26 20:20:00,329 - DEBUG - Starting new HTTPS connection (1): forum.url.com:443

2025-01-26 20:20:00,345 - DEBUG - https://forum.url.com:443 "POST /api/forums/posts HTTP/1.1" 403 69

2025-01-26 20:20:00,346 - DEBUG - Response status code: 403

2025-01-26 20:20:00,346 - DEBUG - Response content: b'{\n "errorCode": "1F295\\/1",\n "errorMessage": "NO_TOPIC"\n}'

2025-01-26 20:20:00,346 - ERROR - Error posting reply: 403 Client Error: Forbidden for url: https://forum.url.com/api/forums/posts

2025-01-26 20:20:00,346 - ERROR - Response content: b'{\n "errorCode": "1F295\\/1",\n "errorMessage": "NO_TOPIC"\n}'


The created API has the correct permissions. The user is in the admin group, so has all the necessary rights.

What may cause the NO_TOPIC error?

Are you sure the topic ID is correct? That error is thrown when the code cannot load the topic from the database:

		/* Get topic */
		try
		{
			$topic = \IPS\forums\Topic::load( \IPS\Request::i()->topic );
		}
		catch ( \OutOfRangeException $e )
		{
			throw new \IPS\Api\Exception( 'NO_TOPIC', '1F295/1', 403 );
		}

Edited by teraßyte

Nevermind. I see the problem in your code now: https://invisioncommunity.com/developers/rest-api

Parameters

For all GET requests, provide parameters in the query string. For PUT and POST requests, all parameters should be sent Form URL Encoded in the body.

You’re sending your request as JSON, which won’t work.

  • Author

It works! Thank you very much!

Recently Browsing 0

  • No registered users viewing this page.