Search

Dark theme | Light theme

March 14, 2025

Nushell Niceties: Getting The HTTP Response Status

Nushell has a built-in command to invoke HTTP requests: http. You don’t need an external tool like curl or httpie to make HTTP requests. There a lot of options to use with the http command. One of them is the --full or shorter -f option to return a table with extra details of the HTTP request and response. The request and response headers, the body and status are returned in the table. You can easily get information from the table with all the default selection options for a table structure.

In the following example you see the table structure that is returned for a http get request with and without the --full option:

> http get https://www.mrhaki.com/nushell.txt # Without --full you get only the response body.
Nushell rocks!
> http get --full https://www.mrhaki.com/nushell.txt # Use --full to get the full response.
╭─────────┬────────────────────────────────────────────────────────────────────────────────────────────╮
│         │ ╭──────────┬─────────────────────────────────────────────────────────────────────────────╮ │
│ headers │ │ request  │ [list 0 items]                                                              │ │
│         │ │          │ ╭────┬───────────────────────────┬────────────────────────────────────────╮ │ │
│         │ │ response │ │  # │           name            │                 value                  │ │ │
│         │ │          │ ├────┼───────────────────────────┼────────────────────────────────────────┤ │ │
│         │ │          │ │  0 │ age                       │ 51                                     │ │ │
│         │ │          │ │  1 │ cache-status              │ "Netlify Edge"; hit                    │ │ │
│         │ │          │ │  2 │ cache-control             │ public,max-age=0,must-revalidate       │ │ │
│         │ │          │ │  3 │ content-type              │ text/plain; charset=UTF-8              │ │ │
│         │ │          │ │  4 │ content-length            │ 14                                     │ │ │
│         │ │          │ │  5 │ x-nf-request-id           │ 01JP9NJNTBY60HF0TR8X2R3J0K             │ │ │
│         │ │          │ │  6 │ server                    │ Netlify                                │ │ │
│         │ │          │ │  7 │ etag                      │ "010320ca2351da56022222fa858c94ae-ssl" │ │ │
│         │ │          │ │  8 │ accept-ranges             │ bytes                                  │ │ │
│         │ │          │ │  9 │ strict-transport-security │ max-age=31536000                       │ │ │
│         │ │          │ │ 10 │ date                      │ Fri, 14 Mar 2025 06:31:00 GMT          │ │ │
│         │ │          │ ╰────┴───────────────────────────┴────────────────────────────────────────╯ │ │
│         │ ╰──────────┴─────────────────────────────────────────────────────────────────────────────╯ │
│ body    │ Nushell rocks!                                                                             │
│ status  │ 200                                                                                        │
╰─────────┴────────────────────────────────────────────────────────────────────────────────────────────╯
>

To get the response status you can retrieve the value from the status field that is in the table:

# With the --full option a table is returned by http get with the fields
# headers, body and status.

# Using get to get the value of the status field.
assert equal (http get --full https://www.mrhaki.com/nushell.txt | get status) 200

# Using select to get a record with the result.
assert equal (http get -f https://www.mrhaki.com/nushell.txt | select status) {status: 200}

Written with Nushell 0.102.0.