diff --git a/src/content/docs/d1/examples/query-d1-from-python-workers.mdx b/src/content/docs/d1/examples/query-d1-from-python-workers.mdx
index 712e9929c7d8a94..18a7b826c6108d1 100644
--- a/src/content/docs/d1/examples/query-d1-from-python-workers.mdx
+++ b/src/content/docs/d1/examples/query-d1-from-python-workers.mdx
@@ -81,7 +81,7 @@ The value of `binding` is how you will refer to your database from within your W
To create a Python Worker, create an empty file at `src/entry.py`, matching the value of `main` in your Wrangler file with the contents below:
```python
-from js import Response
+from workers import Response
async def on_fetch(request, env):
# Do anything else you'd like on request here!
diff --git a/src/content/docs/workers/examples/103-early-hints.mdx b/src/content/docs/workers/examples/103-early-hints.mdx
index b0c0caf6ce04e9e..adcae028ab00206 100644
--- a/src/content/docs/workers/examples/103-early-hints.mdx
+++ b/src/content/docs/workers/examples/103-early-hints.mdx
@@ -111,7 +111,7 @@ export default {
```py
import re
-from js import Response, Headers
+from workers import Response
CSS = "body { color: red; }"
HTML = """
@@ -129,11 +129,11 @@ HTML = """
"""
def on_fetch(request):
if re.search("test.css", request.url):
- headers = Headers.new({"content-type": "text/css"}.items())
- return Response.new(CSS, headers=headers)
+ headers = {"content-type": "text/css"}
+ return Response(CSS, headers=headers)
else:
- headers = Headers.new({"content-type": "text/html","link": "; rel=preload; as=style"}.items())
- return Response.new(HTML, headers=headers)
+ headers = {"content-type": "text/html","link": "; rel=preload; as=style"}
+ return Response(HTML, headers=headers)
```
diff --git a/src/content/docs/workers/examples/ab-testing.mdx b/src/content/docs/workers/examples/ab-testing.mdx
index fa1db299adb5c7c..3b71cca1afaeadf 100644
--- a/src/content/docs/workers/examples/ab-testing.mdx
+++ b/src/content/docs/workers/examples/ab-testing.mdx
@@ -100,7 +100,7 @@ export default {
```py
import random
from urllib.parse import urlparse, urlunparse
-from js import Response, Headers, fetch
+from workers import Response, fetch
NAME = "myExampleWorkersABTest"
@@ -132,8 +132,7 @@ async def on_fetch(request):
res = await fetch(urlunparse(url))
headers = dict(res.headers)
headers["Set-Cookie"] = f'{NAME}={group}; path=/'
- headers = Headers.new(headers.items())
- return Response.new(res.body, headers=headers)
+ return Response(res.body, headers=headers)
return fetch(urlunparse(url))
```
diff --git a/src/content/docs/workers/examples/accessing-the-cloudflare-object.mdx b/src/content/docs/workers/examples/accessing-the-cloudflare-object.mdx
index bef6a8b89eeb4c4..47039f0e893ec93 100644
--- a/src/content/docs/workers/examples/accessing-the-cloudflare-object.mdx
+++ b/src/content/docs/workers/examples/accessing-the-cloudflare-object.mdx
@@ -60,13 +60,14 @@ export default {
```py
import json
-from js import Response, Headers, JSON
+from workers import Response
+from js import JSON
def on_fetch(request):
error = json.dumps({ "error": "The `cf` object is not available inside the preview." })
data = request.cf if request.cf is not None else error
- headers = Headers.new({"content-type":"application/json"}.items())
- return Response.new(JSON.stringify(data, None, 2), headers=headers)
+ headers = {"content-type":"application/json"}
+ return Response(JSON.stringify(data, None, 2), headers=headers)
```
diff --git a/src/content/docs/workers/examples/aggregate-requests.mdx b/src/content/docs/workers/examples/aggregate-requests.mdx
index 5ca1a8673b73d87..b3717a412a2fa2d 100644
--- a/src/content/docs/workers/examples/aggregate-requests.mdx
+++ b/src/content/docs/workers/examples/aggregate-requests.mdx
@@ -61,7 +61,9 @@ export default {
```py
-from js import Response, fetch, Headers, JSON, Promise
+from workers import Response, fetch
+import asyncio
+import json
async def on_fetch(request):
# some_host is set up to return JSON responses
@@ -69,11 +71,11 @@ async def on_fetch(request):
url1 = some_host + "/todos/1"
url2 = some_host + "/todos/2"
- responses = await Promise.all([fetch(url1), fetch(url2)])
- results = await Promise.all(map(lambda r: r.json(), responses))
+ responses = await asyncio.gather(fetch(url1), fetch(url2))
+ results = await asyncio.gather(*(r.json() for r in responses))
- headers = Headers.new({"content-type": "application/json;charset=UTF-8"}.items())
- return Response.new(JSON.stringify(results), headers=headers)
+ headers = {"content-type": "application/json;charset=UTF-8"}
+ return Response.json(results, headers=headers)
```
diff --git a/src/content/docs/workers/examples/alter-headers.mdx b/src/content/docs/workers/examples/alter-headers.mdx
index c3dc7b8ad2871ee..d85457e48c9ea9c 100644
--- a/src/content/docs/workers/examples/alter-headers.mdx
+++ b/src/content/docs/workers/examples/alter-headers.mdx
@@ -80,28 +80,27 @@ export default {
```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
# 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)
```
diff --git a/src/content/docs/workers/examples/auth-with-headers.mdx b/src/content/docs/workers/examples/auth-with-headers.mdx
index 5cd4e327e37d720..2c0cc9a0299df52 100644
--- a/src/content/docs/workers/examples/auth-with-headers.mdx
+++ b/src/content/docs/workers/examples/auth-with-headers.mdx
@@ -79,20 +79,20 @@ export default {
```py
-from js import Response, fetch
+from workers import Response, fetch
async def on_fetch(request):
PRESHARED_AUTH_HEADER_KEY = "X-Custom-PSK"
PRESHARED_AUTH_HEADER_VALUE = "mypresharedkey"
- psk = request.headers.get(PRESHARED_AUTH_HEADER_KEY)
+ psk = request.headers[PRESHARED_AUTH_HEADER_KEY]
if psk == PRESHARED_AUTH_HEADER_VALUE:
# Correct preshared header key supplied. Fetch request from origin.
return fetch(request)
# Incorrect key supplied. Reject the request.
- return Response.new("Sorry, you have supplied an invalid key.", status=403);
+ return Response("Sorry, you have supplied an invalid key.", status=403)
```
diff --git a/src/content/docs/workers/examples/block-on-tls.mdx b/src/content/docs/workers/examples/block-on-tls.mdx
index 7d63a03ae995653..05741afacb2fb6c 100644
--- a/src/content/docs/workers/examples/block-on-tls.mdx
+++ b/src/content/docs/workers/examples/block-on-tls.mdx
@@ -72,12 +72,12 @@ export default {
```py
-from js import Response, fetch
+from workers import Response, fetch
async def on_fetch(request):
tls_version = request.cf.tlsVersion
if tls_version not in ("TLSv1.2", "TLSv1.3"):
- return Response.new("Please use TLS version 1.2 or higher.", status=403)
+ return Response("Please use TLS version 1.2 or higher.", status=403)
return fetch(request)
```
diff --git a/src/content/docs/workers/examples/bulk-redirects.mdx b/src/content/docs/workers/examples/bulk-redirects.mdx
index ffc1f5553ba9a90..e29389c3597dd73 100644
--- a/src/content/docs/workers/examples/bulk-redirects.mdx
+++ b/src/content/docs/workers/examples/bulk-redirects.mdx
@@ -76,7 +76,8 @@ export default {
```py
-from js import Response, fetch, URL
+from workers import Response, fetch
+from urllib.parse import urlparse
async def on_fetch(request):
external_hostname = "examples.cloudflareworkers.com"
@@ -88,8 +89,8 @@ async def on_fetch(request):
"/bulk4": "https://google.com",
}
- url = URL.new(request.url)
- location = redirect_map.get(url.pathname, None)
+ url = urlparse(request.url)
+ location = redirect_map.get(url.path, None)
if location:
return Response.redirect(location, 301)
diff --git a/src/content/docs/workers/examples/conditional-response.mdx b/src/content/docs/workers/examples/conditional-response.mdx
index f4546e414caac80..8ad70c4a9a371a5 100644
--- a/src/content/docs/workers/examples/conditional-response.mdx
+++ b/src/content/docs/workers/examples/conditional-response.mdx
@@ -120,37 +120,38 @@ export default {
```py
import re
-from js import Response, URL, fetch
+from workers import Response
+from urllib.parse import urlparse
async def on_fetch(request):
blocked_hostnames = ["nope.mywebsite.com", "bye.website.com"]
- url = URL.new(request.url)
+ url = urlparse(request.url)
# Block on hostname
if url.hostname in blocked_hostnames:
- return Response.new("Blocked Host", status=403)
+ return Response("Blocked Host", status=403)
# On paths ending in .doc or .xml
- if re.search(r'\.(doc|xml)$', url.pathname):
- return Response.new("Blocked Extension", status=403)
+ if re.search(r'\.(doc|xml)$', url.path):
+ return Response("Blocked Extension", status=403)
# On HTTP method
if "POST" in request.method:
- return Response.new("Response for POST")
+ return Response("Response for POST")
# On User Agent
user_agent = request.headers["User-Agent"] or ""
if "bot" in user_agent:
- return Response.new("Block User Agent containing bot", status=403)
+ return Response("Block User Agent containing bot", status=403)
# On Client's IP address
client_ip = request.headers["CF-Connecting-IP"]
if client_ip == "1.2.3.4":
- return Response.new("Block the IP 1.2.3.4", status=403)
+ return Response("Block the IP 1.2.3.4", status=403)
# On ASN
if request.cf and request.cf.asn == 64512:
- return Response.new("Block the ASN 64512 response")
+ return Response("Block the ASN 64512 response")
# On Device Type
# Requires Enterprise "CF-Device-Type Header" zone setting or
diff --git a/src/content/docs/workers/examples/country-code-redirect.mdx b/src/content/docs/workers/examples/country-code-redirect.mdx
index c5820473fceb57e..8f18795b5918064 100644
--- a/src/content/docs/workers/examples/country-code-redirect.mdx
+++ b/src/content/docs/workers/examples/country-code-redirect.mdx
@@ -82,7 +82,7 @@ export default {
```py
-from js import Response, fetch
+from workers import Response, fetch
async def on_fetch(request):
countries = {
diff --git a/src/content/docs/workers/examples/extract-cookie-value.mdx b/src/content/docs/workers/examples/extract-cookie-value.mdx
index 0ad1ee9cf5b7780..035fcd8bc11a46f 100644
--- a/src/content/docs/workers/examples/extract-cookie-value.mdx
+++ b/src/content/docs/workers/examples/extract-cookie-value.mdx
@@ -58,7 +58,7 @@ export default {
```py
from http.cookies import SimpleCookie
-from js import Response
+from workers import Response
async def on_fetch(request):
# Name of the cookie
@@ -68,9 +68,9 @@ async def on_fetch(request):
if cookie_name in cookies:
# Respond with cookie value
- return Response.new(cookies[cookie_name].value)
+ return Response(cookies[cookie_name].value)
- return Response.new("No cookie with name: " + cookie_name)
+ return Response("No cookie with name: " + cookie_name)
```
diff --git a/src/content/docs/workers/examples/fetch-json.mdx b/src/content/docs/workers/examples/fetch-json.mdx
index b04dae8b0a44b58..eedeaf129abc3a0 100644
--- a/src/content/docs/workers/examples/fetch-json.mdx
+++ b/src/content/docs/workers/examples/fetch-json.mdx
@@ -74,7 +74,8 @@ export default {
```py
-from js import Response, fetch, Headers, JSON
+from workers import Response, fetch
+import json
async def on_fetch(request):
url = "https://jsonplaceholder.typicode.com/todos/1"
@@ -85,15 +86,15 @@ async def on_fetch(request):
content_type = headers["content-type"] or ""
if "application/json" in content_type:
- return (content_type, JSON.stringify(await response.json()))
+ return (content_type, json.dumps(await response.json()))
return (content_type, await response.text())
response = await fetch(url)
content_type, result = await gather_response(response)
- headers = Headers.new({"content-type": content_type}.items())
- return Response.new(result, headers=headers)
+ headers = {"content-type": content_type}
+ return Response(result, headers=headers)
```
diff --git a/src/content/docs/workers/examples/geolocation-app-weather.mdx b/src/content/docs/workers/examples/geolocation-app-weather.mdx
index 608ec84ca958f27..a3a65f984eb761e 100644
--- a/src/content/docs/workers/examples/geolocation-app-weather.mdx
+++ b/src/content/docs/workers/examples/geolocation-app-weather.mdx
@@ -123,7 +123,7 @@ export default {
```py
-from js import Response, Headers, fetch
+from workers import Response, fetch
async def on_fetch(request):
endpoint = "https://api.waqi.info/feed/geo:"
@@ -140,11 +140,11 @@ async def on_fetch(request):
html_content += "This is a demo using Workers geolocation data.
"
html_content += f"You are located at: {latitude},{longitude}.
"
- html_content += f"Based off sensor data from {content.data.city.name}:
"
- html_content += f"The AQI level is: {content.data.aqi}.
"
- html_content += f"The N02 level is: {content.data.iaqi.no2.v}.
"
- html_content += f"The O3 level is: {content.data.iaqi.o3.v}.
"
- html_content += f"The temperature is: {content.data.iaqi.t.v}°C.
"
+ html_content += f"Based off sensor data from {content['data']['city']['name']}:
"
+ html_content += f"The AQI level is: {content['data']['aqi']}.
"
+ html_content += f"The N02 level is: {content['data']['iaqi']['no2']['v']}.
"
+ html_content += f"The O3 level is: {content['data']['iaqi']['o3']['v']}.
"
+ html_content += f"The temperature is: {content['data']['iaqi']['t']['v']}°C.
"
html = f"""
@@ -159,8 +159,8 @@ async def on_fetch(request):