티스토리 뷰

1. Create a JSON file

$ jq -n '{"hello":"world"}' > hello.json

 

jq 라는 툴을 사용한다. 커널에 jq -h 해서 도움말을 보면 아래와 같이 나온다.

jq is a tool for processing JSON inputs, applying the given filter to its JSON text inputs
and producing the filter's results as JSON on standard output.

여기서 -n 태그의 의미는 use null as the single input value;

$ jq -n --arg greeting world '{"hello":$greeting}' > hello.json
--arg a v : set variable $a to value ; greeting 변수에 world값을 할당해서 json파일을 만들수도 있다.

 

 

 

 

2. POST a JSON file with curl

$ curl -X POST -H "Content-Type: application/json" 'http://ubuntu:8999/api' -d @hello.json

 

파일을 post로 보낼 때 반드시 타입을 명시해야한다. json으로 보낸다면 Content-Type: application/json이라 써주자.
또한 현재 JSON을 파일형식으로 보냈기때문에 해당 request값을 받으려면 arg = request.json 이렇게 해야한다.
만약 arg = request.args 라하면 안 받아지니 주의하자.

 

 

 

 

 

3. POST multiparameters with curl

$ curl -X POST -H "Content-Type: application/json" 'http://ubuntu:8999/api?a=11&b=22'

 

이번엔 url이 달라졌다. 아까는 JSON파일로 보냈다면 이번엔 url에 파라미터 값을 할당하여 보냈다.
이 request값을 받으려면 arg = request.args 로 받아야한다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함