2nd message not posted to Discourse server

AI-generated summary

lenny1 is experiencing an issue with sending multiple posts to a Discourse forum server using the Discourse API. The first post is sent successfully, but the second post is not received by the server. However, when a 20-second delay is added between the posts, both are received successfully.

simon suggests that the issue might be due to a rate limit, and recommends checking the Discourse error logs and the response from the Discourse server. packman also mentions that the rate limit might be specific to the account being used to post the messages.

lenny1 confirms that the issue is due to a rate limit and resolves it by changing the rate limit in the Admin page to 0. However, lenny1 also asks if it’s possible to set the rate limit for a specific account only, to which simon responds that this is not possible, but notes that staff accounts are exempt from the rate limit.

Im trying to send more than one message to my Discourse forum server using this API:

My java codes:

public void test() 
		throws UnirestException, JsonMappingException, JsonProcessingException, InterruptedException {
	
	Unirest.config().cookieSpec(CookieSpecs.IGNORE_COOKIES);
	
	Map<String, String> header = new HashMap<>();
	header.put("Content-Type", "application/json");
	header.put("Api-Key", [redacted]);
	header.put("Api-Username", [redacted]);	    
    header.put("Accept", "application/json");

	
	Unirest.post(URL + "/posts.json")
			.headers(header)
			.queryString("title","1st Msg")
			.queryString("raw", "Message 1")
			.queryString("target_recipients", "Kenny")
			.queryString("archetype", "private_message")
			.asJson();

	
   // TimeUnit.SECONDS.sleep(20);
	
	
	Unirest.post(URL + "/posts.json")
			.headers(header)
			.queryString("title","2nd Msg")
			.queryString("raw", "Message 2")
			.queryString("target_recipients", "Kenny")
			.queryString("archetype", "private_message")
			.asJson();
}

In my test above, both messages were sent successfully (there wasn’t any exception). The problem is after login Discourse account I saw only the 1st message was received but the 2nd message was missing.

Later, I found that if I put a delay of 20 seconds in between them (i.e. in the comment), then only both can be received by the server.

Whats wrong?

My version:

  • Discourse 3.1.0.beta2
  • Java 11
  • unirest-java 3.14.2

Hi @lenny1 – I redacted parts of your post because they contained sensitive information. Please don’t post API keys or passwords etc publicly.

You might be hitting a rate limit. If that is the cause, Discourse should be returning a 429 response. There might be some information in your Discourse error logs about this. They are found at Admin / Logs / Error Logs.

If you are not seeing any errors there, try logging the response that Discourse sends to the server that’s running your Java code. That might give some useful information.

Depending on which account you’re using to post the messages you may be hitting ‘rate limit create topic’ or ’ rate limit create post’.

I’m just working on a script that sends 11 posts consecutively (within about a second) and that’s working OK, but they are being sent by the system account which might not have rate limits applied to it.

Thanks for the reply.
Yes, I got response from Discourse as below:
“You’re creating topics a bit too quickly. Please wait 15 seconds before trying again.”
I solved the problem by changing the rate limit in Admin page, set the post and topic creation to 0.
Now my question is possible to set the rate limit for specific account only?

No, the rate limit cannot be set for a specific account. The rate limit create topic and rate limit create post settings are not applied to staff accounts though. Possibly that could help for your use case.