Skip to content

Commit c46b187

Browse files
committed
feat: default to 204 No Content for empty response body
1 parent fb96fe7 commit c46b187

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Set HTTP response status and headers using nushell's pipeline metadata:
218218

219219
```nushell
220220
"body" | metadata set --merge {'http.response': {
221-
status: <number> # Optional, HTTP status code (default: 200)
221+
status: <number> # Optional, defaults to 204 if body is empty, 200 otherwise
222222
headers: { # Optional, HTTP headers
223223
<key>: <value> # Single value: "text/plain"
224224
<key>: [<value>, <value>] # Multiple values: ["cookie1=a", "cookie2=b"]

src/handler.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,11 @@ async fn build_normal_response(
325325
) -> HTTPResult {
326326
let request_id = guard.request_id();
327327
let (inferred_content_type, http_meta, body) = pipeline_result;
328-
let status = http_meta.status.unwrap_or(200);
328+
let status = match (http_meta.status, &body) {
329+
(Some(s), _) => s,
330+
(None, ResponseTransport::Empty) => 204,
331+
(None, _) => 200,
332+
};
329333
let mut builder = hyper::Response::builder().status(status);
330334
let mut header_map = hyper::header::HeaderMap::new();
331335

src/test_handler.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ async fn test_content_type_precedence() {
238238
resp5.headers().get("content-type").is_none(),
239239
"Empty body should not have Content-Type header"
240240
);
241+
242+
// 6. Empty body defaults to 204 No Content
243+
let req6 = Request::builder()
244+
.uri("/")
245+
.body(Empty::<Bytes>::new())
246+
.unwrap();
247+
let engine = Arc::new(ArcSwap::from_pointee(test_engine(r#"{|req| null}"#)));
248+
let resp6 = handle(engine.clone(), None, no_trusted_proxies(), req6)
249+
.await
250+
.unwrap();
251+
assert_eq!(resp6.status(), 204, "Empty body should default to 204");
241252
}
242253

243254
#[tokio::test]

0 commit comments

Comments
 (0)