Skip to content

Commit

Permalink
Automatically adding Content-Type header depending on request body type
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Oct 16, 2024
1 parent 5cdbbac commit 0639adf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 16 additions & 1 deletion src/posting/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from posting.collection import (
Collection,
Cookie,
Header,
HttpRequestMethod,
Options,
RequestModel,
Expand Down Expand Up @@ -657,7 +658,21 @@ def build_request_model(self, request_options: Options) -> RequestModel:
# We ensure elsewhere that the we can only "open" requests, not collection nodes.
assert not isinstance(open_request, Collection)

request_editor_args = self.request_editor.to_request_model_args()
headers = self.headers_table.to_model()
if request_body := request_editor_args.get("body"):
header_names_lower = {header.name.lower(): header for header in headers}
# Don't add the content type header if the user has explicitly set it.
if (
request_body.content_type is not None
and "content-type" not in header_names_lower
):
headers.append(
Header(
name="content-type",
value=request_body.content_type,
)
)
return RequestModel(
name=self.request_metadata.request_name,
path=open_request.path if open_request else None,
Expand All @@ -674,7 +689,7 @@ def build_request_model(self, request_options: Options) -> RequestModel:
else []
),
scripts=self.request_scripts.to_model(),
**self.request_editor.to_request_model_args(),
**request_editor_args,
)

def load_request_model(self, request_model: RequestModel) -> None:
Expand Down
11 changes: 4 additions & 7 deletions src/posting/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,14 @@ def apply_template(self, variables: dict[str, Any]) -> None:

def to_httpx(self, client: httpx.AsyncClient) -> httpx.Request:
"""Convert the request model to an httpx request."""
headers = httpx.Headers(
[(header.name, header.value) for header in self.headers if header.enabled]
)
return client.build_request(
method=self.method,
url=self.url,
**(self.body.to_httpx_args() if self.body else {}),
headers=httpx.Headers(
[
(header.name, header.value)
for header in self.headers
if header.enabled
]
),
headers=headers,
params=httpx.QueryParams(
[(param.name, param.value) for param in self.params if param.enabled]
),
Expand Down
1 change: 0 additions & 1 deletion tests/sample-collections/scripts/my_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def setup(posting: Posting) -> None:

def on_request(request: httpx.Request, posting: Posting) -> None:
new_header = "Foo-Bar-Baz!!!!!"
request.headers["X-Custom-Header"] = new_header
print(f"Set header to {new_header!r}!")
posting.notify(
message="Hello from my_script.py!",
Expand Down

0 comments on commit 0639adf

Please sign in to comment.