Skip to content
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

Automatically adding Content-Type header depending on request body type #116

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading