Can't send PM via API (HTTPUnprocessableEntity 422)


(AstonJ) #1

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:

irb> require 'uri'
=> true

irb> require 'net/http'
=> true

irb> Net::HTTP.post_form(URI.parse("https://forum.com/posts?api_key=12345&api_username=MyForum"), {title: "test", raw: "test", archetype: "private_message", target_usernames: "Me"})

=> #<Net::HTTPUnprocessableEntity 422 Unprocessable Entity readbody=true>
irb(main):004:0> 

I can’t even get this to work:

curl https://forum.com/posts?api_key=12345&api_username=MyForum&archetype=private_message&title=title&raw=message&target_usernames=Me

Any idea what’s going on?


(AstonJ) #2

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.)


(AstonJ) #3

…and it’s stopped working again :frowning:

All I did was add the following:

msg << "Here's the email template:"
msg << "\n \n"
msg << "Hi username!"
msg << "\n \n"
msg << "Guess what!? You won last month's giveaway :-)"
msg << "\n \n"
msg << "Please send me your:"
msg << "\n \n"
msg << "Full name:"
msg << "\n \n"
msg << "Email address:"
msg << "\n \n"
msg << "Country of residence:"
msg << "\n \n"
msg << "And I'll send your details over."
msg << "\n \n"
msg << "All the best,"
msg << "\n \n"
msg << "Aston"
msg << "\n \n"

The constructed msg is what’s sent as raw in the params.


(Joffrey Jaffeux) #4

given it’s a 422, you are probably not hitting a rate limit.

Can you show the full code please ?


(Joffrey Jaffeux) #5

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.


(AstonJ) #6

The full code is in the first post (use Ruby with IRB to run it).

Sam has said that PMs are just posts and it’s fine to use the API:

:slight_smile:


(Joffrey Jaffeux) #7

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.


(Blake Erickson) #8

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"]}

(Blake Erickson) #9

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

(Joffrey Jaffeux) #10

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”.


(Blake Erickson) #11

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).