diff --git a/src/posting/app.py b/src/posting/app.py index 25efdd4b..5d9a6aa5 100644 --- a/src/posting/app.py +++ b/src/posting/app.py @@ -31,6 +31,7 @@ from posting.collection import ( Collection, Cookie, + Header, HttpRequestMethod, Options, RequestModel, @@ -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, @@ -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: diff --git a/src/posting/collection.py b/src/posting/collection.py index c313e9f6..c1672a94 100644 --- a/src/posting/collection.py +++ b/src/posting/collection.py @@ -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] ), diff --git a/tests/sample-collections/scripts/my_script.py b/tests/sample-collections/scripts/my_script.py index 7b52f29e..a06faf7f 100644 --- a/tests/sample-collections/scripts/my_script.py +++ b/tests/sample-collections/scripts/my_script.py @@ -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!",