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

Unexcepted result in content-type #30

Merged
merged 1 commit into from
Jun 28, 2021
Merged

Unexcepted result in content-type #30

merged 1 commit into from
Jun 28, 2021

Conversation

MarcHagen
Copy link
Contributor

@MarcHagen MarcHagen commented Jun 27, 2021

resp.headers.get("content-type") can return application/json; charset=utf-8 which fails the check.

Please see esphome/issues#2183
This could fix: esphome/issues#2183 , esphome/issues#2193

The problem is here, ports is not a array but plain text.

.then((ports) => ports.filter((port) => port.port !== "OTA"));

Then fetchApi doesn't appear to return a javascript array but regular text.
Which is wierd because this is checked here:

return resp.headers.get("content-type") === "application/json"
? resp.json()
: resp.text();

This is not a browser cache problem.
/serial-ports does indeed return a json text, and the content-type: application/json; charset=utf-8 header.

And that's where it goes wrong.
In my testing, resp.headers.get("content-type") returned: application/json; charset=utf-8 which is not the same as application/json.
I'm not quite sure where the charset=utf-8 comes from, but this PR fixes the issue quite easly.

Another posibility is to force resp.json() wrapped in a try / catch.

try {
    return resp.json();
} catch(err) {
   return resp.text();
}

resp.headers.get("content-type") can return `application/json; charset=utf-8` which fails the check.
Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@balloob balloob merged commit 2f3e04d into esphome:main Jun 28, 2021
@MarcHagen MarcHagen deleted the patch-1 branch June 28, 2021 05:01
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

Successfully merging this pull request may close these issues.

"LOGS" Button Does Not Function in MS Edge in ESPHome Add-On
2 participants