Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

406 Error #594

Closed
distler opened this issue Jul 9, 2022 · 13 comments
Closed

406 Error #594

distler opened this issue Jul 9, 2022 · 13 comments

Comments

@distler
Copy link

distler commented Jul 9, 2022

Very odd. For the past day or so, the Ruby client has been returning a 406 (Not Acceptable) error.

/usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/response/raise_error.rb:32:in `on_complete': the server responded with status 406 (Faraday::ClientError)
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/middleware.rb:19:in `block in call'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/response.rb:61:in `on_complete'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/middleware.rb:18:in `call'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday_middleware-1.2.0/lib/faraday_middleware/response_middleware.rb:36:in `call'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday_middleware-1.2.0/lib/faraday_middleware/request/encode_json.rb:26:in `call'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
	from /usr/local/lib/ruby/gems/3.1.0/gems/faraday-1.10.0/lib/faraday/connection.rb:281:in `post'
	from /usr/local/lib/ruby/gems/3.1.0/gems/tesla_api-3.1.0/lib/tesla_api/client.rb:195:in `post'
	from /usr/local/lib/ruby/gems/3.1.0/gems/tesla_api-3.1.0/lib/tesla_api/vehicle.rb:77:in `wake_up'
	from /usr/local/bin/tesla_SoC.rb:26:in `<main>'

Worked fine before, so I'm not sure what changed. (This is with latest Git HEAD.)

@alandtse
Copy link
Contributor

alandtse commented Jul 9, 2022

It's not language specific at this point. We're seeing it in Python. The 406 is returning a not_a_JSON_request error:

406: {"response":null,"error":"not_a_JSON_request","error_description":""}

@distler
Copy link
Author

distler commented Jul 9, 2022

Thanks for confirming.

There was an update to the Tesla App a few days ago ("Minor fixes and improvements"). Presumably the release note should have read "Update for changes to the API."

@alandtse
Copy link
Contributor

alandtse commented Jul 9, 2022

It's not a public facing API. I doubt they'd mention it to the public. It'd be interesting to check to see if they've ever mentioned an API change publicly.

@bojihao
Copy link

bojihao commented Jul 9, 2022

I assume we missed a header Content-Type: application/json

@SylvainGa
Copy link

SylvainGa commented Jul 10, 2022

Nope, I'm starting to receive 406 errors starting today and I checked my code and every web request do have Content-Type: application/json

The authentication call proceeds successfully but the 'wake_up' call returns 406.

If already awaken, I can retrieve the vehicle data but any commands to change something returns 406. So 'Get' commands work but Post commands returns 406.

Edit: Scratch that. I had "HTTP_RESPONSE_CONTENT_TYPE_JSON" for response type (using Garmin API) but NOT for the request. Once I added ""Content-Type: application/json" to the Post header, it started to work again :-)

@distler
Copy link
Author

distler commented Jul 10, 2022

It's not the Content-Type header that we failed to set. It's the Accept header. (Recall what a 406 error means.)

--- a/lib/tesla_api/client.rb     2022-07-08 23:27:31.000000000 -0500
+++ b/lib/tesla_api/client.rb 2022-07-09 21:43:20.000000000 -0500
@@ -35,6 +35,7 @@
         # conn.response :logger, nil, {headers: true, bodies: true}
         conn.request :json
         conn.response :json
+        conn.headers['Accept'] = 'application/json'
         conn.response :raise_error
         conn.request :retry, retry_options if retry_options # Must be registered after :raise_error
         conn.adapter Faraday.default_adapter

fixes the problem.

D'oh!

@SylvainGa
Copy link

It was working before. Looks like Tesla is now more strict at what is passed in the header of a POST command.

@bojihao
Copy link

bojihao commented Jul 10, 2022

I am new to Tesla so not familiar with the history of Tesla API. Was Tesla using query params or form data instead of json body in the past?

@SylvainGa
Copy link

I'm also somewhat new to this. Started last fall. My app (on a Garmin watch) was working fine until today without the "Content-Type: application/json" in the POST Header. Now any POST commands fail without that line in the Header.

@albahari
Copy link

It appears that since the API update, the application/json content header is required even for posts that don't include content (such as wake_up, charge_start and charge_stop).

distler added a commit to distler/tesla-api that referenced this issue Jul 10, 2022
The Tesla API now complains if we don't set the Accept header.
@Panda88CO
Copy link

can someone show how to add this fix to Python

@timdorr
Copy link
Owner

timdorr commented Jul 11, 2022

I fixed this at the client level for the next release. In the meantime, just use this to create your client:

client = TeslaApi::Client.new(client_options: {headers: {"Accept" => "application/json"}})

@SylvainGa
Copy link

Thanks @albahari. It's not relevant here but, what you said helped me fix my app on my Garmin watch (Monkey C). I added a dummy content (ie { "dummy" => "dummy" } to the MakeWebRequest calls that had an empty content and I can now control my car from my Garmin watch again :-)

So it needed two fixes:

        Communications.makeWebRequest(
            url,
            {
                "dummy" => "dummy"  <=== THIS BLOCK
            },
            {
                :method => Communications.HTTP_REQUEST_METHOD_POST,
                :headers => {
                    "Authorization" => _token,
                    "User-Agent" => "Tesla-Link for Garmin",
                    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON   <=== THIS LINE
                },
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
            },
            notify
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants