Add credentials: include and Cookie header to prevent 401 error #8600
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
When connecting to a gradio app with authentication using JS client, there will be 401 error when interacting with the API.
When running it locally with the JS client:

When running it on browser (Chrome) by including it via CDN (as mentioned in the documentation) with the following code:




It shows 401 error with message "Not authenticated".
Which by looking an the request headers, the credential cookies are not being sent as part of the request:
This PR propose the change to add the required headers (credentials: "include" & Cookie) when Client.stream() is being called.
Headers and Credentials are both being added as running the code files in terminal with credentials: "include" only will also result in 401 error. While running it in browser with Cookie header only will also result in 401, Adding both headers will give the best compatibility when using the client on different platform.
Reproduction
It should fail with 401 error. By changing the stream function, it should work normally.
🎯 PRs Should Target Issues
Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.
Not adhering to this guideline will result in the PR being closed.
Tests
PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests:
bash scripts/run_all_tests.sh
You may need to run the linters:
bash scripts/format_backend.sh
andbash scripts/format_frontend.sh