curl
์ ๊ฐ์ด ์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ต์
์ ์ถ๊ฐํ๊ธฐ๋ง ํ๋ฉด ์์ฒญ์ด ๊ฐ๋จํฉ๋๋ค.
http :/api/user -d 'MyRawData...'
๋๋ถ๋ถ์ ๊ฒฝ์ฐ JSON ๋๋ ์์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด _"์์ฒญ ํญ๋ชฉ"_์ ์ฌ์ฉํ์ฌ ์ํํ ์ ์๋ค๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค.
http :/api/hey say=Hello to=me โฆ
๊ทธ๋ฆฌ๊ณ ์ฝํ ์ธ ์ ํ์ ๋ฐ๋ผ ์ ์ ํ ํ์์ผ๋ก ๋ณํ๋ฉ๋๋ค. ๋ณด๋ผ JSON ๋๋ ์์ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
echo 'MyRawData...' | http :/api/hey
๊ทธ๋ฌ๋ ์ด๊ฒ์ ๋น์ค์ฉ์ ์
๋๋ค. HTTPie์ ์ฃผ์ ์์ด๋์ด๋ _cURL๊ณผ ๊ฐ์ ์ธ๊ฐ์ฉ ๋๊ตฌ์
๋๋ค_ , ์ด ๊ฒฝ์ฐ๋ ๊ทธ ์์น๊ณผ๋ ๊ฑฐ๋ฆฌ๊ฐ ์์ต๋๋ค. ์ฌ์ค ์ด์ ์์์ curl
๊ฐ HTTPie๋ณด๋ค ๋ ์ค์ฉ์ ์
๋๋ค. ๋จ์ํ ์ต์
์ด ์๋ค๋ ์ด์ ๋ก ํ๋ ์ด์์ ๋ช
๋ น์ ์ถ๊ฐํ๊ณ |
<
์ ๊ฐ์ ๋ชป์๊ธด ๋ฌธ์๋ก ํ์ดํํ๋ ๊ฒ์ _์ธ๊ฐ ์นํ์ ์ธ_ ๊ฒ์ฒ๋ผ ๋ค๋ฆฌ์ง ์์ต๋๋ค.
-d
์ต์
์ http
์ ์ถ๊ฐํ๋ฉด ๋ฌด์์ด ๋ฌธ์ ์
๋๊น?
http์ -d ์ต์ ์ ์ถ๊ฐํ๋ฉด ์ด๋ค ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๊น?
_ํนํ_ ์๋ชป๋ ๊ฒ์ ์์ต๋๋ค. ๋๋ ๋ฐฐ๊ด์ด ๋ ๊นจ๋ํ๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๊ณ ๊ฐ์ ์ผ์ ์ํํ๋ ๋ฐฉ๋ฒ์ด ํ๋๋ฟ์ผ ๋ ๊ฐ๋ ฅํ๊ฒ ์ ํธํฉ๋๋ค. ๋ฐฐ๊ด์ ๋ฐ๋ก ์ด ๋ชฉ์ ์ ์ํด ์กด์ฌํ๋ฉฐ(์: _data_๋ฅผ ํ๋ก๊ทธ๋จ์ ์ ๋ฌํ๊ธฐ ์ํด), ์ดํดํ๊ธฐ ์ฝ๊ณ ๋ณดํธ์ ์ด๋ฉฐ ๋ชจํธํ์ง ์์ต๋๋ค. ๋ชจ๋ ๊ด์ฐฎ์ CLI ๋๊ตฌ๋ ํ์ดํ( curl
์ ์ธ)์ ์ง์ํ๋ฏ๋ก ๊ฐ๋
์ ํ ๋ฒ๋ง ๋ฐฐ์ฐ๋ฉด ๋ฉ๋๋ค.
๋น๊ตํ๋ค:
์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ณดํธ์ ์ธ ๋ฐฉ๋ฒ์ ๋ฆฌ๋๋ ์ ๋ ํ์ค ์ ๋ ฅ(ํ์ค ์ ๋ ฅ)์ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ์ด๋ฌํ ๋ฐ์ดํฐ๋ ๋ฒํผ๋ง๋ ๋ค์ ์์ฒญ ๋ณธ๋ฌธ์ผ๋ก ์ฌ์ฉ๋๋ ์ถ๊ฐ ์ฒ๋ฆฌ๊ฐ ์์ต๋๋ค.
-d, --data <data>
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser
does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass
the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F,
--form.
-d, --data is the same as --data-ascii. --data-raw is almost the same but does not have a special
interpretation of the @ character. To post data purely binary, you should instead use the --data-binary
option. To URL-encode the value of a form field you may use --data-urlencode.
If any of these options is used more than once on the same command line, the data pieces specified will
be merged together with a separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would gener-
ate a post chunk that looks like 'name=daniel&skill=lousy'.
If you start the data with the letter @, the rest should be a file name to read the data from, or - if
you want curl to read the data from stdin. Multiple files can also be specified. Posting data from a
file named 'foobar' would thus be done with --data @foobar. When --data is told to read from a file
like that, carriage returns and newlines will be stripped out. If you don't want the @ character to
have a special interpretation use --data-raw instead.
๋ค, ์ปค๋งจ๋ ๋ผ์ธ ๋๊ตฌ์์ ํ์ดํ์ ์ง์ํ๋ ๊ฒ์ด ์ข์ ๊ธฐ๋ฅ์ด๋ผ๋ ๋ฐ ๋์ํฉ๋๋ค. ๋ํ ํ์ดํ์ ์ง์ํ๋ ๋ช
๋ น์ค ๋๊ตฌ( Mongotail )๋ฅผ ๋ง๋ค์๋๋ฐ ์์งํ curl
์ธ์ง ๋ชฐ๋์ต๋๋ค. ์ง์ํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ Unix ์์ฝ์์คํ
์์ ์๋ ค์ง ๊ฑฐ์ ๋ชจ๋ CLI ๋๊ตฌ๊ฐ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋ชจ๋ ์ง์ํ๊ธฐ ๋๋ฌธ์ ๋ ๊ธฐ๋ฅ์ ๋ชจ๋ ์ง์ํ๋ค๊ณ ํด์ ๋ณต์ก์ฑ์ด ์ถ๊ฐ๋์ง๋ ์์ต๋๋ค. ์. cat
, grep
, find
, tail
...
์ธ๊ธํ ๋ช
๋ น์ ์ผ๋ฐ์ ์ผ๋ก stdin
ํตํด ํ์ผ ์ด๋ฆ ์ธ์ ๋ชฉ๋ก ๋๋ ์์ ์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ค์ ๋ฐ์ดํฐ๋ฅผ ์ธ์๋ก ๋ฐ์๋ค์ด์ง ๋ง์ญ์์ค. ์ธ์๋ฅผ ํตํด ์์ ๋ฐ์ดํฐ๋ฅผ ์๋ฝํ๋ ๊ฒ์ ๋งค์ฐ ๋๋ฌธ ์ผ์
๋๋ค.
(์ด์ ์ฃผ์์์ ์์ฑํ ๋ด์ฉ์ ๋ช
ํํ ํ๊ธฐ: curl
๋ stdin์ ์ง์ํ์ง๋ง ์๋ฅผ ๋ค์ด --data-binary @-
๋ก ์ฝ๋๋ก ๋ช
์์ ์ผ๋ก ์ง์ํด์ผ ํฉ๋๋ค.)
์ด ๋ฌธ์ ์ ๊ด๋ จ๋ ๋ฒ๊ทธ๋ฅผ ์ ๊ณ ํ๊ธฐ ์ํด ์ฌ๊ธฐ์ ์์ผ๋ฏ๋ก ๋ฒ๊ทธ๊ฐ ์๋๋ผ ์ค๊ณ๋ ๋๋ก ์๋ํ๋ ๊ฒ์ผ ์ ์์ต๋๋ค.
"curl"๋์ "httpie"๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝํ bash ์คํฌ๋ฆฝํธ๊ฐ ์์ต๋๋ค. ์์ฒญ์ http ์๋ฒ์ ๋ํ ๋น ๋ณธ๋ฌธ POST์
๋๋ค. ์ด ์คํฌ๋ฆฝํธ๋ฅผ docker exec -i ${container} bash -x
์ ํ์ดํํ์ฌ ์คํํฉ๋๋ค.
http POST
๋ช
๋ น์ด ๋ํํ ์
ธ์์ ์คํ๋ ๋ ์ ๋๋ก ์๋ํ๋ ๋์ ์คํฌ๋ฆฝํธ๊ฐ ์ฆ์ ์ข
๋ฃ๋๋๋ก ํ๋ค๋ ๊ฒ์ ์์์ฐจ๋ฆฌ๋ ๋ฐ ์ด๋ ค์์ ๊ฒช์์ต๋๋ค.
http
docker exec
์์ stdin ์ ์ฝ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ด๊ฒ์ ํผํ๊ธฐ ์ํด " echo -n
"๋ฅผ ํ์ดํํด์ผ ํ๋ค๋ ๊ฒ์ด ์ด์ํ๊ฒ ๋ณด์
๋๋ค.
#!/bin/bash
echo "STARTING..."
echo -n | http POST ... # this replaces: curl -XPOST --data-binary '' ...
echo "Without the 'echo -n' above this statement would not be reached."
echo "DONE"
( @jamshid POST
๋จ์ํ http POST httpbin.org/post
$ ๊ฐ ์๋ ๋น ๋ณธ๋ฌธ . ์คํฌ๋ฆฝํธ์์ HTTPie ์ฌ์ฉ์ ๋ํ ์ธ๋ถ ์ฌํญ์ ์ฝ์ผ์ญ์์ค โ --ignore-stdin
์ต์
์ ํฌํจํ๋ ค๊ณ ํฉ๋๋ค. ์ด๊ฒ์ ๊ด๋ จ์ด ์๋ ๋ฌธ์ ์
๋๋ค. , ํ์ง๋ง ํ์ํ ๊ฒฝ์ฐ ์ฌ๊ธฐ์ ํ์ ํ๋ ๋์ ์ ๋ฌธ์ ๋ฅผ ์ฌ์ญ์์ค.)
15.1 ํ์ผ ์ด๋ฆ์ ์์ฒญ ๋ฐ์ดํฐ๊ฐ ์ด ๋ฌธ์ ์ ์๋ ์์ฒญ์ ํฌํจํ๋ค๊ณ ์๊ฐํ๋ ๊ฒ์ด ๋ง์ต๋๊น? ์ด ๋ฌธ์ ๋ ๋ซ์ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
์ ์ณ๋๊ณ , ๋๋ ๋ด XML ํ์ผ์ ์ค ๋ฐ๊ฟ์ด ์ ์ง๋์ง ์๋ ์ด์ ๋ฅผ ์์๋ด๊ธฐ ์ํด 3์๊ฐ ์ ๋๋ฅผ ํผํ์ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ด์ ์ ์ HTTPie์ ๋ํด ์์๋๋ผ๋ฉด ์ข์์ ๊ฒ์
๋๋ค. (๋ด ์ฑ์ธ ์ค ์์๋๋ฐ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ๋๋ ค๋ฉด --data-binary
์ต์
์ ์ฌ์ฉํด์ผ ํ๋ curl์ด์์ต๋๋ค.) HTTPie์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค!
๊ทธ๋ ์ง ์์ต๋๋ค, @DavidOliver . @mrsarm ์ด ํ์ผ ๋ด์ฉ์ด ์๋ ๋งค๊ฐ๋ณ์์์ ๋ฌธ์์ด์ ์ ๋ฌํ ์ ์๋๋ก ์์ฒญํ์ต๋๋ค.
+1
@jakubroztocil ๊ธฐ๋ฅ์ด ์๋ MR์ ์๋ฝํ์๊ฒ ์ต๋๊น?
http POST example.org <<< "foo bar"
๋๋ http POST example.org < file.name
๋ฅผ ํ ์ ์์ต๋๋ค.
http://api/hey say=์๋ ํ์ธ์=๋ โฆ
http POST example.org <<< "foo bar"
๋๋http POST example.org < file.name
๋ฅผ ํ ์ ์์ต๋๋ค.
powershell์์๋ ์๋ํ์ง ์๋ ๊ฒ ๊ฐ์์ต๋๋ค. 'raw body data' | http post :8080/api/events
๋ powershell์์ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค.
ํ์ง๋ง ์ฌ์ ํ ์์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ ์ํด -d, --data
๋๋ ์ด์ ์ ์ฌํ ๊ฒ์ ์ํฉ๋๋ค.
๋ฌธ์์ ๋ฐ๋ฅด๋ฉด "Bash here string"์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
http example.com/ <<<'{"name": "John"}'
UI ์ธก๋ฉด์์ ์ด ์ต์ ์ ์๋ฏธ๊ฐ ์์ต๋๋ค.
์ด์ํ์ง๋ง ์ ํจํ ์ฌ์ฉ ์ฌ๋ก์ธ ๋น json ๊ฐ์ฒด( {}
)๋ฅผ ๋ณด๋ด๋ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
@minusf : ์ด์ํ์ง๋ง ์ ํจํ ์ฌ์ฉ ์ฌ๋ก์ธ ๋น json ๊ฐ์ฒด(
{}
)๋ฅผ ๋ณด๋ด๋ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
$ echo '{}' | http httpbin.org/post
๋ฆฌ๋๋ ์ ํ๋ ๋์ ๊ฐํ์ ๋ฒ๋ฆฌ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
$ echo 20 | http POST httpbin.org/post
์ ์ถ๋ ๋ฐ์ดํฐ๋ "data": "20\n"
์
๋๋ค.
@hahattan echo
์ -n
๋ฅผ ์ฌ์ฉํ์ฌ ํํ ์ค ๋ฐ๊ฟ ๋ฌธ์๋ฅผ ์ธ์ํ์ง ์๋๋ก ์ง์ํ ์ ์์ต๋๋ค.
$ echo -n foo | http httpbin.org/post
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
http POST example.org <<< "foo bar"
๋๋http POST example.org < file.name
๋ฅผ ํ ์ ์์ต๋๋ค.