Anyone know what I am doing wrong here please? It was working fine, then I don’t know what I did but it stopped working. I’ve stripped it back to this and it’s still not working:
This seems to be working again now - is there some kind of limit or temp bans if too many requests are sent within a specific time-frame? (I was testing so sending quite a few requests.)
also I’ve been looking at Discourse API Documentation, where do you see the documentation to send private messages ? I only see a section on how to list private messages.
Ok I tried various things, and there’s definitely something weird going on. I tried sending a PM to me on meta.discourse.org (using the UI) while inspecting xhr requests, and sometimes I got 422 trying to do so.
I haven’t found a common pattern, the only thing I’m changing is the text sent, and it’s always over the char limit, and yet sometimes I get a 422.
anyway you can look at the response body and not just the status code? The response body should still return json with an “errors” section that could explain what is going on:
example:
{"action":"create_post","errors":["Your error message here for a 422 status code"]}
also I think your your curl command is wrong because you aren’t specifying a POST request and/or your messages content isn’t url encoded.
Your curl command should be changed to look like this:
curl -X POST -d api_key="" -d api_username="discourse1" -d target_usernames="discourse2" -d archetype="private_message" -d title="my sample title" -d raw="This is a post to a topic." http://localhost:3000/posts
I thought about it later, and I’m pretty sure he is hitting a 422 while doing his tests because he is sending the same requests, so the content body of his 422 is probably “Body is too similar to what you recently posted”.
I thought the same thing, and so I tried sending the same request over and over and it still returns a 200 with the same id (does not create a new pm).
I am successfully able to create a PM just fine via the api as a user with a non-admin api key:
curl -i -sS -X POST "http://localhost:3000/posts.json" \
-H "Api-Key: 768dae6a45015f4220ad195ef9a8e8c4b48687a5b0bead26a6bbe2cf1a0f8b70" \
-H "Api-Username: steve" \
-F "title=I need to create a random title" \
-F "target_usernames=blake.erickson" \
-F "raw=This is the body of my pm that I am sending. I hope it is long enough" \
-F "archetype=private_message"
HTTP/1.1 200 OK
So far, all off the 422 responses that I was able to trigger have returned a json response in this format with an error message:
{"action":"create_post","errors":["Body is too similar to what you recently posted"]}
Some of the other error messages I was able to generate are:
One of the users you are sending this message to could not be found.
Title is too short (minimum is 2 characters)
and
Body is too short (minimum is 20 characters)","Body seems unclear, is it a complete sentence?
The “Body is too similar…” error will happen if a user sends the same pm to multiple different users, but an admin can successfully send the same pm body to multiple different users.
As long as we return an error message with the 422 I would say this endpoint is working as advertised.