2nd message not posted to Discourse server

Im trying to send more than one message to my Discourse forum server using this API:
https://docs.discourse.org/#tag/Posts/operation/listPosts

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.

2 Likes

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.

3 Likes

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.

3 Likes

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.

2 Likes

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