Skip to content

Commit 756dc87

Browse files
fix(workers): correctly use multipart while uploading scripts (#2661)
* fix(workers): correctly use multipart while uploading scripts * fix(examples): use correct json serialising method --------- Co-authored-by: Robert Craigie <[email protected]>
1 parent 420b061 commit 756dc87

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

examples/workers/script_upload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"""
2626

2727
import os
28-
import json
2928

3029
from cloudflare import Cloudflare, BadRequestError
3130

@@ -88,7 +87,7 @@ def main() -> None:
8887
},
8988
)
9089
print("Script Upload success!")
91-
print(json.dumps(script, indent=2))
90+
print(script.to_json(indent=2))
9291
except BadRequestError as err:
9392
print("Script Upload failure!")
9493
print(err)

src/cloudflare/resources/workers/scripts/scripts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ def update(
205205
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
206206
if not script_name:
207207
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
208+
# It should be noted that the actual Content-Type header that will be
209+
# sent to the server will contain a `boundary` parameter, e.g.
210+
# multipart/form-data; boundary=---abc--
211+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
208212
return self._put(
209213
f"/accounts/{account_id}/workers/scripts/{script_name}",
210214
body=maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@@ -214,6 +218,7 @@ def update(
214218
extra_query=extra_query,
215219
extra_body=extra_body,
216220
timeout=timeout,
221+
multipart_syntax='json',
217222
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
218223
),
219224
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
@@ -449,6 +454,10 @@ async def update(
449454
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
450455
if not script_name:
451456
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
457+
# It should be noted that the actual Content-Type header that will be
458+
# sent to the server will contain a `boundary` parameter, e.g.
459+
# multipart/form-data; boundary=---abc--
460+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
452461
return await self._put(
453462
f"/accounts/{account_id}/workers/scripts/{script_name}",
454463
body=await async_maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@@ -459,6 +468,7 @@ async def update(
459468
extra_body=extra_body,
460469
timeout=timeout,
461470
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
471+
multipart_syntax='json',
462472
),
463473
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
464474
)

src/cloudflare/resources/workers_for_platforms/dispatch/namespaces/scripts/scripts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ def update(
161161
raise ValueError(f"Expected a non-empty value for `dispatch_namespace` but received {dispatch_namespace!r}")
162162
if not script_name:
163163
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
164+
# It should be noted that the actual Content-Type header that will be
165+
# sent to the server will contain a `boundary` parameter, e.g.
166+
# multipart/form-data; boundary=---abc--
167+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
164168
return self._put(
165169
f"/accounts/{account_id}/workers/dispatch/namespaces/{dispatch_namespace}/scripts/{script_name}",
166170
body=maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@@ -170,6 +174,7 @@ def update(
170174
extra_query=extra_query,
171175
extra_body=extra_body,
172176
timeout=timeout,
177+
multipart_syntax='json',
173178
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
174179
),
175180
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
@@ -370,6 +375,10 @@ async def update(
370375
raise ValueError(f"Expected a non-empty value for `dispatch_namespace` but received {dispatch_namespace!r}")
371376
if not script_name:
372377
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
378+
# It should be noted that the actual Content-Type header that will be
379+
# sent to the server will contain a `boundary` parameter, e.g.
380+
# multipart/form-data; boundary=---abc--
381+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
373382
return await self._put(
374383
f"/accounts/{account_id}/workers/dispatch/namespaces/{dispatch_namespace}/scripts/{script_name}",
375384
body=await async_maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@@ -379,6 +388,7 @@ async def update(
379388
extra_query=extra_query,
380389
extra_body=extra_body,
381390
timeout=timeout,
391+
multipart_syntax='json',
382392
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
383393
),
384394
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),

0 commit comments

Comments
 (0)