Home Explore Blog CI



nushell

6th chunk of `cookbook/http.md`
d7f6353f354d9cbce8a9742b1d1f23a63cb5cbf82b551f7b0000000100001503
# => │ json    │                                                                                                       │
# => │ url     │ https://httpbin.org/post                                                                              │
# => ╰─────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────╯
```

If the file happens to be a text file, you may need to additionally convert it to binary data before sending it. This can be done using the `into binary` command.

```nu
http post https://httpbin.org/post --content-type "multipart/form-data" {
  doc: (open -r ~/Downloads/README.txt | into binary),
  description: "Documentation file"
}
# => ╭─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
# => │ args    │ {record 0 fields}                                                                                        │
# => │ data    │                                                                                                          │
# => │         │ ╭──────┬───────────────────────────────────────────────────────────────────────────────────────────────╮ │
# => │ files   │ │ doc  │ To use Nu plugins, use the register command to tell Nu where to find the plugin. For example: │ │
# => │         │ │      │                                                                                               │ │
# => │         │ │      │ > register ./nu_plugin_query                                                                  │ │
# => │         │ ╰──────┴───────────────────────────────────────────────────────────────────────────────────────────────╯ │
# => │         │ ╭─────────────┬────────────────────╮                                                                     │
# => │ form    │ │ description │ Documentation file │                                                                     │
# => │         │ ╰─────────────┴────────────────────╯                                                                     │
# => │         │ ╭─────────────────┬────────────────────────────────────────────────────────────────────╮                 │
# => │ headers │ │ Accept          │ */*                                                                │                 │
# => │         │ │ Accept-Encoding │ gzip                                                               │                 │
# => │         │ │ Content-Length  │ 476                                                                │                 │
# => │         │ │ Content-Type    │ multipart/form-data; boundary=f872d6c3-7937-426d-b266-de562b777e1d │                 │
# => │         │ │ Host            │ httpbin.org                                                        │                 │
# => │         │ │ User-Agent      │ nushell                                                            │                 │
# => │         │ │ X-Amzn-Trace-Id │ Root=1-66b28eef-4998c6ab0ef5becb19ca7f6f                           │                 │
# => │         │ ╰─────────────────┴────────────────────────────────────────────────────────────────────╯                 │
# => │ json    │                                                                                                          │
# => │ url     │ https://httpbin.org/post                                                                                 │
# => ╰─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────╯
```

Title: Uploading Text Files as Binary Data with multipart/form-data
Summary
Demonstrates how to upload a text file as binary data using the `http post` command with `multipart/form-data` content type in Nushell. It highlights the necessity of converting the text file into binary format using `into binary` before sending it. The example uploads a README.txt file and provides 'description' as an additional form field, showcasing the structure of the resulting HTTP request, including headers and form data.