Error 405 On POST

203 views
Skip to first unread message

Jonathan Kruse

unread,
Jul 27, 2023, 10:44:28 AM7/27/23
to rabbitmq-users
I hav a legacy application that has access to .NET. We are basically re-creating this:

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

            httpClient.DefaultRequestHeaders.Add("Authorization", "Basic 1234567890");


            string jsonBody = "{\"payload\":\"hello world from sage 100\",\"payload_encoding\":\"string\",\"properties\":{},\"routing_key\":\"CustomerOrder_SageSync\"}";


            StringContent stringContent = new StringContent(jsonBody, System.Text.Encoding.UTF8, "application/json");


            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(new Uri("http://12.34.56.78:8111/api/exchanges/%2f/amq.default/publish"), stringContent);

We are getting error 405 Method Not Allowed. This works from Visual Studio and from PostMan. Not sure what else to try as I use this same exact code in the legacy application to successfully make API calls to several other APIs.

Luke Bakken

unread,
Jul 27, 2023, 11:50:27 AM7/27/23
to rabbitmq-users
Hello,

I have added your code to this repo and have made it runnable:


In the future, OSS project maintainers like Team RabbitMQ greatly appreciate it when users take the time to provide their code in a repository that can be cloned, compiled, and run. It may not seem like a big deal, but I did spend about 10 minutes getting your code into a new console project, checked in, and runnable. Multiply that by the number of times we assist users, every day, for free, and you can see why it's nice to not have to do that.

Your code works fine on my machine. I'm using RabbitMQ 3.12.2 / Erlang 26.0.2 on Windows 11



I suggest checking your RabbitMQ logs. Also, it would be good to let us know what version of RabbitMQ and Erlang you're using.

Finally, a traffic capture can ensure that the HTTP request you expect to be reaching RabbitMQ is actually the one it's getting.

Thanks,
Luke

Jonathan Kruse

unread,
Jul 28, 2023, 9:56:10 AM7/28/23
to rabbitmq-users
After testing various things, we believe the %2f in the URI is getting converted to a slash. We found this by accident by trying various clients and when we pasted the URI it did this. This will produce a 405 on a POST if you just replace %2f with /

Is there any way to remove that %2f from the URI?

Luke Bakken

unread,
Jul 28, 2023, 11:27:49 AM7/28/23
to rabbitmq-users
I just added code to my test project to print the URI. As you can see, it is not converted, and the request is successful:

C:\Users\bakkenl\development\lukebakken\rabbitmq-users-xGUuCAeUq_4 [main ≡]
> dotnet run
URI: http://localhost:15672/api/exchanges/%2f/amq.default/publish
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Cache-Control: no-cache
  Content-Security-Policy: script-src 'self' 'unsafe-eval' 'unsafe-inline'; object-src 'self'
  Date: Fri, 28 Jul 2023 15:24:21 GMT
  Server: Cowboy
  Vary: accept, accept-encoding, origin
  Content-Length: 16
  Content-Type: application/json
}


Something in your environment (a proxy or load balancer) is trying to be smart with HTTP requests and un-escaping the %2F escape sequence before the request makes it to RabbitMQ. The %2F is a necessary part of the REST request because it specifies the vhost to use - in this case, the default vhost that is named /

Jonathan Kruse

unread,
Jul 28, 2023, 11:37:47 AM7/28/23
to rabbitmq-users
We think it has to do with the machine being on .NET 4.0
https://stackoverflow.com/questions/35875376/uri-absoluteuri-vs-uri-originalstring

This Stackoverflow describes the exact issue we are seeing.
Reply all
Reply to author
Forward
0 new messages