-
Notifications
You must be signed in to change notification settings - Fork 7k
Updates Python Workers examples to use new Python SDK API #18596
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,28 +80,27 @@ export default { | |
</TabItem> <TabItem label="Python" icon="seti:python"> | ||
|
||
```py | ||
from js import Response, fetch | ||
from workers import Response, fetch | ||
|
||
async def on_fetch(request): | ||
response = await fetch("https://example.com") | ||
|
||
# Clone the response so that it's no longer immutable | ||
new_response = Response.new(response.body, response) | ||
# Grab the response headers so they can be modified | ||
new_headers = response.headers | ||
Comment on lines
-88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
# Add a custom header with a value | ||
new_response.headers.append( | ||
"x-workers-hello", | ||
"Hello from Cloudflare Workers" | ||
) | ||
|
||
new_headers["x-workers-hello"] = "Hello from Cloudflare Workers" | ||
|
||
# Delete headers | ||
new_response.headers.delete("x-header-to-delete") | ||
new_response.headers.delete("x-header2-to-delete") | ||
if "x-header-to-delete" in new_headers: | ||
del new_headers["x-header-to-delete"] | ||
if "x-header2-to-delete" in new_headers: | ||
del new_headers["x-header2-to-delete"] | ||
|
||
# Adjust the value for an existing header | ||
new_response.headers.set("x-header-to-change", "NewValue") | ||
new_headers["x-header-to-change"] = "NewValue" | ||
|
||
return new_response | ||
return Response(response.body, headers=new_headers) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if we just returned the original response? Since it looks like we modified its headers in place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't modify them in-place, we get a new instance of the headers when we access |
||
``` | ||
|
||
</TabItem> </Tabs> | ||
|
Uh oh!
There was an error while loading. Please reload this page.