-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
README.md
2598 lines (1865 loc) · 81.3 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<div class='hidden-website'>
# HTTPie documentation
</div>
HTTPie (pronounced _aitch-tee-tee-pie_) is a command-line HTTP client.
Its goal is to make CLI interaction with web services as human-friendly as possible.
HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers.
The `http` & `https` commands allow for creating and sending arbitrary HTTP requests.
They use simple and natural syntax and provide formatted and colorized output.
<div class='hidden-website'>
## About this document
This documentation is best viewed at [httpie.io/docs](https://httpie.org/docs).
You can select your corresponding HTTPie version as well as run examples directly from the browser using a [termible.io](https://termible.io?utm_source=httpie-readme) embedded terminal.
If you are reading this on GitHub, then this text covers the current *development* version.
You are invited to submit fixes and improvements to the docs by editing [this file](https://github.com/httpie/httpie/blob/master/docs/README.md).
</div>
## Main features
- Expressive and intuitive syntax
- Formatted and colorized terminal output
- Built-in JSON support
- Forms and file uploads
- HTTPS, proxies, and authentication
- Arbitrary request data
- Custom headers
- Persistent sessions
- Wget-like downloads
- Linux, macOS, Windows, and FreeBSD support
- Plugins
- Documentation
- Test coverage
## Installation
<div data-installation-instructions>
<!--
THE INSTALLATION SECTION IS GENERATED
Do not edit here, but in docs/installation/.
-->
- [Universal](#universal)
- [macOS](#macos)
- [Windows](#windows)
- [Linux](#linux)
- [FreeBSD](#freebsd)
### Universal
#### PyPI
Please make sure you have Python 3.7 or newer (`python --version`).
```bash
# Install httpie
$ python -m pip install --upgrade pip wheel
$ python -m pip install httpie
```
```bash
# Upgrade httpie
$ python -m pip install --upgrade pip wheel
$ python -m pip install --upgrade httpie
```
### macOS
#### Homebrew
To install [Homebrew](https://brew.sh/), see [its installation](https://docs.brew.sh/Installation).
```bash
# Install httpie
$ brew update
$ brew install httpie
```
```bash
# Upgrade httpie
$ brew update
$ brew upgrade httpie
```
#### MacPorts
To install [MacPorts](https://www.macports.org/), see [its installation](https://www.macports.org/install.php).
```bash
# Install httpie
$ port selfupdate
$ port install httpie
```
```bash
# Upgrade httpie
$ port selfupdate
$ port upgrade httpie
```
### Windows
#### Chocolatey
To install [Chocolatey](https://chocolatey.org/), see [its installation](https://chocolatey.org/install).
```bash
# Install httpie
$ choco install httpie
```
```bash
# Upgrade httpie
$ choco upgrade httpie
```
### Linux
#### Snapcraft (Linux)
To install [Snapcraft](https://snapcraft.io/), see [its installation](https://snapcraft.io/docs/installing-snapd).
```bash
# Install httpie
$ snap install httpie
```
```bash
# Upgrade httpie
$ snap refresh httpie
```
#### Linuxbrew
To install [Linuxbrew](https://docs.brew.sh/Homebrew-on-Linux), see [its installation](https://docs.brew.sh/Homebrew-on-Linux#install).
```bash
# Install httpie
$ brew update
$ brew install httpie
```
```bash
# Upgrade httpie
$ brew update
$ brew upgrade httpie
```
#### Debian and Ubuntu
Also works for other Debian-derived distributions like MX Linux, Linux Mint, deepin, Pop!_OS, KDE neon, Zorin OS, elementary OS, Kubuntu, Devuan, Linux Lite, Peppermint OS, Lubuntu, antiX, Xubuntu, etc.
```bash
# Install httpie
$ curl -SsL https://packages.httpie.io/deb/KEY.gpg | apt-key add -
$ curl -SsL -o /etc/apt/sources.list.d/httpie.list https://packages.httpie.io/deb/httpie.list
$ apt update
$ apt install httpie
```
```bash
# Upgrade httpie
$ apt update
$ apt upgrade httpie
```
#### Fedora
```bash
# Install httpie
$ dnf install httpie
```
```bash
# Upgrade httpie
$ dnf upgrade httpie
```
#### CentOS and RHEL
Also works for other RHEL-derived distributions like ClearOS, Oracle Linux, etc.
```bash
# Install httpie
$ yum install epel-release
$ yum install httpie
```
```bash
# Upgrade httpie
$ yum upgrade httpie
```
#### Arch Linux
Also works for other Arch-derived distributions like ArcoLinux, EndeavourOS, Artix Linux, etc.
```bash
# Install httpie
$ pacman -Syu httpie
```
```bash
# Upgrade httpie
$ pacman -Syu
```
#### Single binary executables
Get the standalone HTTPie Linux executables when you don't want to go through the full installation process
```bash
# Install httpie
$ https --download packages.httpie.io/binaries/linux/http-latest -o http
$ chmod +x ./http
```
```bash
# Upgrade httpie
$ https --download packages.httpie.io/binaries/linux/http-latest -o http
```
### FreeBSD
#### FreshPorts
```bash
# Install httpie
$ pkg install www/py-httpie
```
```bash
# Upgrade httpie
$ pkg upgrade www/py-httpie
```
<!-- /GENERATED SECTION -->
</div>
### Unstable version
You can also install the latest unreleased development version directly from the `master` branch on GitHub.
It is a work-in-progress of a future stable release so the experience might be not as smooth.
You can install it on Linux, macOS, Windows, or FreeBSD with `pip`:
```bash
$ python -m pip install --upgrade https://github.com/httpie/httpie/archive/master.tar.gz
```
Or on macOS, and Linux, with Homebrew:
```bash
$ brew uninstall --force httpie
$ brew install --HEAD httpie
```
And even on macOS, and Linux, with Snapcraft:
```bash
$ snap remove httpie
$ snap install httpie --edge
```
Verify that now you have the [current development version identifier](https://github.com/httpie/httpie/blob/master/httpie/__init__.py#L6) with the `.dev0` suffix, for example:
```bash
$ http --version
# 3.X.X.dev0
```
## Usage
Hello World:
```bash
$ https httpie.io/hello
```
Synopsis:
```bash
$ http [flags] [METHOD] URL [ITEM [ITEM]]
```
See also `http --help` (and for systems where man pages are available, you can use `man http`).
### Examples
Custom [HTTP method](#http-method), [HTTP headers](#http-headers) and [JSON](#json) data:
```bash
$ http PUT pie.dev/put X-API-Token:123 name=John
```
Submitting [forms](#forms):
```bash
$ http -f POST pie.dev/post hello=World
```
See the request that is being sent using one of the [output options](#output-options):
```bash
$ http -v pie.dev/get
```
Build and print a request without sending it using [offline mode](#offline-mode):
```bash
$ http --offline pie.dev/post hello=offline
```
Use [GitHub API](https://developer.github.com/v3/issues/comments/#create-a-comment) to post a comment on an [issue](https://github.com/httpie/httpie/issues/83) with [authentication](#authentication):
```bash
$ http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body='HTTPie is awesome! :heart:'
```
Upload a file using [redirected input](#redirected-input):
```bash
$ http pie.dev/post < files/data.json
```
Download a file and save it via [redirected output](#redirected-output):
```bash
$ http pie.dev/image/png > image.png
```
Download a file `wget` style:
```bash
$ http --download pie.dev/image/png
```
Use named [sessions](#sessions) to make certain aspects of the communication persistent between requests to the same host:
```bash
$ http --session=logged-in -a username:password pie.dev/get API-Key:123
```
```bash
$ http --session=logged-in pie.dev/headers
```
Set a custom `Host` header to work around missing DNS records:
```bash
$ http localhost:8000 Host:example.com
```
## HTTP method
The name of the HTTP method comes right before the URL argument:
```bash
$ http DELETE pie.dev/delete
```
Which looks similar to the actual `Request-Line` that is sent:
```http
DELETE /delete HTTP/1.1
```
In addition to the standard methods (`GET`, `POST`, `HEAD`, `PUT`, `PATCH`, `DELETE`, etc.), you can use custom method names, for example:
```bash
$ http AHOY pie.dev/post
```
There are no restrictions regarding which request methods can include a body. You can send an empty `POST` request:
```bash
$ http POST pie.dev/post
```
You can also make `GET` requests containing a body:
```bash
$ http GET pie.dev/get hello=world
```
### Optional `GET` and `POST`
The `METHOD` argument is optional, and when you don’t specify it, HTTPie defaults to:
- `GET` for requests without body
- `POST` for requests with body
Here we don’t specify any request data, so both commands will send the same `GET` request:
```bash
$ http GET pie.dev/get
```
```bash
$ http pie.dev/get
```
Here, on the other hand, we do have some data, so both commands will make the same `POST` request:
```bash
$ http POST pie.dev/post hello=world
```
```bash
$ http pie.dev/post hello=world
```
## Request URL
The only information HTTPie needs to perform a request is a URL.
The default scheme is `http://` and can be omitted from the argument:
```bash
$ http example.org
# → http://example.org
```
HTTPie also installs an `https` executable, where the default scheme is `https://`:
```bash
$ https example.org
# → https://example.org
```
When you paste a URL into the terminal, you can even keep the `://` bit in the URL argument to quickly convert the URL into an HTTPie call just by adding a space after the protocol name.
```bash
$ https ://example.org
# → https://example.org
```
```bash
$ http ://example.org
# → http://example.org
```
### Querystring parameters
If you find yourself manually constructing URLs with querystring parameters on the terminal, you may appreciate the `param==value` syntax for appending URL parameters.
With that, you don’t have to worry about escaping the `&` separators for your shell. Additionally, any special characters in the parameter name or value get automatically URL-escaped (as opposed to the parameters specified in the full URL, which HTTPie doesn’t modify).
```bash
$ http https://api.github.com/search/repositories q==httpie per_page==1
```
```http
GET /search/repositories?q=httpie&per_page=1 HTTP/1.1
```
You can even retrieve the `value` from a file by using the `param==@file` syntax. This would also effectively strip the newlines from the end. See [file based separators](#file-based-separators) for more examples.
```bash
$ http pie.dev/get text==@files/text.txt
```
### URL shortcuts for `localhost`
Additionally, curl-like shorthand for localhost is supported.
This means that, for example, `:3000` would expand to `http://localhost:3000`
If the port is omitted, then port 80 is assumed.
```bash
$ http :/foo
```
```http
GET /foo HTTP/1.1
Host: localhost
```
```bash
$ http :3000/bar
```
```http
GET /bar HTTP/1.1
Host: localhost:3000
```
```bash
$ http :
```
```http
GET / HTTP/1.1
Host: localhost
```
### Other default schemes
When HTTPie is invoked as `https` then the default scheme is `https://` (`$ https example.org` will make a request to `https://example.org`).
You can also use the `--default-scheme <URL_SCHEME>` option to create shortcuts for other protocols than HTTP (possibly supported via [plugins](https://pypi.org/search/?q=httpie)). Example for the [httpie-unixsocket](https://github.com/httpie/httpie-unixsocket) plugin:
```bash
# Before
$ http http+unix://%2Fvar%2Frun%2Fdocker.sock/info
```
```bash
# Create an alias
$ alias http-unix='http --default-scheme="http+unix"'
```
```bash
# Now the scheme can be omitted
$ http-unix %2Fvar%2Frun%2Fdocker.sock/info
```
### `--path-as-is`
The standard behavior of HTTP clients is to normalize the path portion of URLs by squashing dot segments as a typically filesystem would:
```bash
$ http -v example.org/./../../etc/password
```
```http
GET /etc/password HTTP/1.1
```
The `--path-as-is` option allows you to disable this behavior:
```bash
$ http --path-as-is -v example.org/./../../etc/password
```
```http
GET /../../etc/password HTTP/1.1
```
## Request items
There are a few different *request item* types that provide a convenient
mechanism for specifying HTTP headers, JSON and form data, files,
and URL parameters. This is a very practical way of constructing
HTTP requests from scratch on the CLI.
Each *request item* is simply a key/value pair separated with the following
characters: `:` (headers), `=` (data field, e.g., JSON, form), `:=` (raw data field)
`==` (query parameters), `@` (file upload).
```bash
$ http PUT pie.dev/put \
X-Date:today \ # Header
token==secret \ # Query parameter
name=John \ # Data field
age:=29 # Raw JSON
```
| Item Type | Description |
|-------------------------------------------------------------:|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| HTTP Headers `Name:Value` | Arbitrary HTTP header, e.g. `X-API-Token:123` |
| URL parameters `name==value` | Appends the given name/value pair as a querystring parameter to the URL. The `==` separator is used. |
| Data Fields `field=value` | Request data fields to be serialized as a JSON object (default), to be form-encoded (with `--form, -f`), or to be serialized as `multipart/form-data` (with `--multipart`) |
| Raw JSON fields `field:=json` | Useful when sending JSON and one or more fields need to be a `Boolean`, `Number`, nested `Object`, or an `Array`, e.g., `meals:='["ham","spam"]'` or `pies:=[1,2,3]` (note the quotes) |
| File upload fields `field@/dir/file`, `field@file;type=mime` | Only available with `--form`, `-f` and `--multipart`. For example `screenshot@~/Pictures/img.png`, or `'cv@cv.txt;type=text/markdown'`. With `--form`, the presence of a file field results in a `--multipart` request |
Note that the structured data fields aren’t the only way to specify request data:
[raw request body](#raw-request-body) is a mechanism for passing arbitrary request data.
### File based separators
Using file contents as values for specific fields is a very common use case, which can be achieved through adding the `@` suffix to
the operators above. For example instead of using a static string as the value for some header, you can use `:@` operator
to pass the desired value from a file.
```bash
$ http POST pie.dev/post \
X-Data:@files/text.txt # Read a header from a file
token==@files/text.txt # Read a query parameter from a file
name=@files/text.txt # Read a data field’s value from a file
bookmarks:=@files/data.json # Embed a JSON object from a file
```
### Escaping rules
You can use `\` to escape characters that shouldn’t be used as separators (or parts thereof). For instance, `foo\==bar` will become a data key/value pair (`foo=` and `bar`) instead of a URL parameter.
Often it is necessary to quote the values, e.g. `foo='bar baz'`.
If any of the field names or headers starts with a minus (e.g. `-fieldname`), you need to place all such items after the special token `--` to prevent confusion with `--arguments`:
```bash
$ http pie.dev/post -- -name-starting-with-dash=foo -Unusual-Header:bar
```
```http
POST /post HTTP/1.1
-Unusual-Header: bar
Content-Type: application/json
{
"-name-starting-with-dash": "foo"
}
```
## JSON
JSON is the *lingua franca* of modern web services, and it is also the **implicit content type** HTTPie uses by default.
Simple example:
```bash
$ http PUT pie.dev/put name=John email=john@example.org
```
```http
PUT / HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json
Host: pie.dev
{
"name": "John",
"email": "john@example.org"
}
```
### Default behavior
If your command includes some data [request items](#request-items), they are serialized as a JSON object by default. HTTPie also automatically sets the following headers, both of which can be overwritten:
| Header | Value |
|---------------:|-------------------------------|
| `Content-Type` | `application/json` |
| `Accept` | `application/json, */*;q=0.5` |
### Explicit JSON
You can use `--json, -j` to explicitly set `Accept` to `application/json` regardless of whether you are sending data (it’s a shortcut for setting the header via the usual header notation: `http url Accept:'application/json, */*;q=0.5'`).
Additionally, HTTPie will try to detect JSON responses even when the `Content-Type` is incorrectly `text/plain` or unknown.
### Non-string JSON fields
Non-string JSON fields use the `:=` separator, which allows you to embed arbitrary JSON data into the resulting JSON object.
Additionally, text and raw JSON files can also be embedded into fields using `=@` and `:=@`:
```bash
$ http PUT pie.dev/put \
name=John \ # String (default)
age:=29 \ # Raw JSON — Number
married:=false \ # Raw JSON — Boolean
hobbies:='["http", "pies"]' \ # Raw JSON — Array
favorite:='{"tool": "HTTPie"}' \ # Raw JSON — Object
bookmarks:=@files/data.json \ # Embed JSON file
description=@files/text.txt # Embed text file
```
```http
PUT /person/1 HTTP/1.1
Accept: application/json, */*;q=0.5
Content-Type: application/json
Host: pie.dev
{
"age": 29,
"hobbies": [
"http",
"pies"
],
"description": "John is a nice guy who likes pies.",
"married": false,
"name": "John",
"favorite": {
"tool": "HTTPie"
},
"bookmarks": {
"HTTPie": "https://httpie.org",
}
}
```
The `:=`/`:=@` syntax is JSON-specific. You can switch your request to `--form` or `--multipart`,
and string, float, and number values will continue to be serialized (as string form values).
Other JSON types, however, are not allowed with `--form` or `--multipart`.
### Nested JSON
If your use case involves sending complex JSON objects as part of the request body,
HTTPie can help you build them right from your terminal. You still use the existing
data field operators (`=`/`:=`) but instead of specifying a top-level field name (like `key=value`),
you specify a path declaration. This tells HTTPie where and how to put the given value inside an object:
```bash
http pie.dev/post \
platform[name]=HTTPie \
platform[about][mission]='Make APIs simple and intuitive' \
platform[about][homepage]=httpie.io \
platform[about][homepage]=httpie.io \
platform[about][stars]:=54000 \
platform[apps][]=Terminal \
platform[apps][]=Desktop \
platform[apps][]=Web \
platform[apps][]=Mobile
```
```json
{
"platform": {
"name": "HTTPie",
"about": {
"mission": "Make APIs simple and intuitive",
"homepage": "httpie.io",
"stars": 54000
},
"apps": [
"Terminal",
"Desktop",
"Web",
"Mobile"
]
}
}
```
#### Introduction
Let’s start with a simple example, and build a simple search query:
```bash
$ http --offline --print=B pie.dev/post \
category=tools \
search[type]=id \
search[id]:=1
```
In the example above, the `search[type]` is an instruction for creating an object called `search`, and setting the `type` field of it to the given value (`"id"`).
Also note that, just as the regular syntax, you can use the `:=` operator to directly pass raw JSON values (e.g, numbers in the case above).
```json
{
"category": "tools",
"search": {
"id": 1,
"type": "id"
}
}
```
Building arrays is also possible, through `[]` suffix (an append operation). This tells HTTPie to create an array in the given path (if there is not one already), and append the given value to that array.
```bash
$ http --offline --print=B pie.dev/post \
category=tools \
search[type]=keyword \
search[keywords][]=APIs \
search[keywords][]=CLI
```
```json
{
"category": "tools",
"search": {
"keywords": [
"APIs",
"CLI"
],
"type": "keyword"
}
}
```
If you want to explicitly specify the position of elements inside an array,
you can simply pass the desired index as the path:
```bash
$ http --offline --print=B pie.dev/post \
category=tools \
search[type]=keyword \
search[keywords][1]=APIs \
search[keywords][0]=CLI
```
```json
{
"category": "tools",
"search": {
"keywords": [
"CLIs",
"API"
],
"type": "keyword"
}
}
```
If there are any missing indexes, HTTPie will nullify them in order to create a concrete object that can be sent:
```bash
$ http --offline --print=B pie.dev/post \
category=tools \
search[type]=platforms \
search[platforms][]=Terminal \
search[platforms][1]=Desktop \
search[platforms][3]=Mobile
```
```json
{
"category": "tools",
"search": {
"platforms": [
"Terminal",
"Desktop",
null,
"Mobile"
],
"type": "platforms"
}
}
```
It is also possible to embed raw JSON to a nested structure, for example:
```bash
$ http --offline --print=B pie.dev/post \
category=tools \
search[type]=platforms \
'search[platforms]:=["Terminal", "Desktop"]' \
search[platforms][]=Web \
search[platforms][]=Mobile
```
```json
{
"category": "tools",
"search": {
"platforms": [
"Terminal",
"Desktop",
"Web",
"Mobile"
],
"type": "platforms"
}
}
```
And just to demonstrate all of these features together, let’s create a very deeply nested JSON object:
```bash
$ http PUT pie.dev/put \
shallow=value \ # Shallow key-value pair
object[key]=value \ # Nested key-value pair
array[]:=1 \ # Array — first item
array[1]:=2 \ # Array — second item
array[2]:=3 \ # Array — append (third item)
very[nested][json][3][httpie][power][]=Amaze # Nested object
```
#### Advanced usage
##### Top level arrays
If you want to send an array instead of a regular object, you can simply
do that by omitting the starting key:
```bash
$ http --offline --print=B pie.dev/post \
[]:=1 \
[]:=2 \
[]:=3
```
```json
[
1,
2,
3
]
```
You can also apply the nesting to the items by referencing their index:
```bash
http --offline --print=B pie.dev/post \
[0][type]=platform [0][name]=terminal \
[1][type]=platform [1][name]=desktop
```
```json
[
{
"type": "platform",
"name": "terminal"
},
{
"type": "platform",
"name": "desktop"
}
]
```
##### Escaping behavior
Nested JSON syntax uses the same [escaping rules](#escaping-rules) as
the terminal. There are 3 special characters, and 1 special token that you can escape.
If you want to send a bracket as is, escape it with a backslash (`\`):
```bash
$ http --offline --print=B pie.dev/post \
'foo\[bar\]:=1' \
'baz[\[]:=2' \
'baz[\]]:=3'
```
```json
{
"baz": {
"[": 2,
"]": 3
},
"foo[bar]": 1
}
```
If you want to send the literal backslash character (`\`), escape it with another backslash:
```bash
$ http --offline --print=B pie.dev/post \
'backslash[\\]:=1'
```
```json
{
"backslash": {
"\\": 1
}
}
```
A regular integer in a path (e.g `[10]`) means an array index; but if you want it to be treated as
a string, you can escape the whole number by using a backslash (`\`) prefix.
```bash
$ http --offline --print=B pie.dev/post \
'object[\1]=stringified' \
'object[\100]=same' \
'array[1]=indexified'
```
```json
{
"array": [
null,
"indexified"
],
"object": {
"1": "stringified",
"100": "same"
}
}
```
##### Guiding syntax errors
If you make a typo or forget to close a bracket, the errors will guide you to fix it. For example:
```bash
$ http --offline --print=B pie.dev/post \
'foo[bar]=OK' \
'foo[baz][quux=FAIL'
```
```console
HTTPie Syntax Error: Expecting ']'
foo[baz][quux
^
```
You can follow to given instruction (adding a `]`) and repair your expression.
##### Type safety
Each container path (e.g., `x[y][z]` in `x[y][z][1]`) has a certain type, which gets defined with
the first usage and can’t be changed after that. If you try to do a key-based access to an array or
an index-based access to an object, HTTPie will error out: