david-caro / python-foreman

Small low level python wrapper around Foreman API
GNU General Public License v2.0
58 stars 37 forks source link

"Accept: application/json; version=2" triggers 500 Internal Server Error in Foreman when using URL without "/api/" #47

Closed pief closed 9 years ago

pief commented 9 years ago

I noticed that python-foreman's usage of application/json; version=2 has an influence on Foreman's behavior when using URLs such as https://127.0.0.1/ without the /api/ part:

# curl -k --header "Accept: application/json" https://127.0.0.1/
{"error":"Authentication error"}
# curl -k --header "Accept: application/json; version=2" https://127.0.0.1/
<html><body>You are being <a href="https://127.0.0.1/users/login">redirected</a>.</body>
# curl -k --header "Accept: application/json; version=2" https://127.0.0.1/users/login
<!DOCTYPE html>
<html>
<head>
  <title>We're sorry, but something went wrong (500)</title>
[...]

This does not happen when one uses newer, "correct" URLs such as "https://127.0.0.1/api/":

#  curl -k --header "Accept: application/json" https://127.0.0.1/api/
{"message":"Unable to authenticate user "}
# curl -k --header "Accept: application/json; version=2" https://127.0.0.1/api/
{
  "error": {"message":"Unable to authenticate user "}
}

While this is, of course, Foreman's behavior and not python-foreman's behavior, python-foreman could (eg. via a regular expression) make sure that the supplied URL contains the /api/ part.

At the very least the documentation should be updated because the current (outdated, but still online) documentation at http://david-caro.github.io/python-foreman/basic.html#connect still refers to using an URL without the /api/ part, thus possibly irritating new python-foreman users.

pief commented 9 years ago

It gets worse: when supplying an URL with the /api/ part to the Foreman constructor, get_foreman_version breaks because it expects the front page's HTML to parse the Foreman version, even if the constructor is called with the api_version parameter.

It seems to me that the version=2 part causes trouble here?

david-caro commented 9 years ago

Where does python-foreman use the url without the api?

pief commented 9 years ago

Sorry, I should have included code examples for calling python-foreman. However for some reason or another, I can't reproduce the behavior any longer, so I'm closing this.