Open elct9620 opened 11 hours ago
Can you try again using lower case headers, e.g. authorization
, content-type
and user-agent
?
I had tried user-agent
and it did not work, too.
I also changed the user agent, which is unrelated to the user agent that is sent to Discord.
Sync do
Async::HTTP::Internet.post(
Async::HTTP::Endpoint.parse("https://discord.com/api/v#{API_VERSION}/channels/#{CHANNEL_ID}/messages"),
{
'Content-Type' => 'application/json',
'Authorization' => "Bot #{ENV['DISCORD_BOT_TOKEN']}",
'User-Agent' => 'Faraday 2.17.2' # use Faraday user-agent is not work
},
{ content: "Hello World" }.to_json
)
end
Faraday and Net::HTTP both only support HTTP/1 so that may be a difference.
What response are you getting back?
Just got 400 bad request message from Cloudflare
=> #<Async::HTTP::Protocol::HTTP2::Response:0x63d8 status=400>
irb(main):002> res.body.read
=> "<html>\r\n<head><title>400 Bad Request</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<hr><center>cloudflare</center>\r\n</body>\r\n</html>\r\n"
Okay, I'll investigate.
I am trying to build a Discord application, the document explains the user agent is necessary to prevent Cloudflare from returning 400 errors.
To make a PoC I write below Ruby code.
The response is 400 bad request from Cloudflare.
However, if using
Net::HTTP
(ref: discorb) orFaraday
is works correctly.On my website, if the bot filter is enabled the
Net::HTTP
isn't work. But Discord still acceptsNet::HTTP
requests, I guess there are some behaviors detected by Cloudflare and blocked it.