[Linux] cURL 사용시 데이터(text/plain, x-www-form-urlencoded 또는 json) 같이 보내기

cURL 사용시 데이터(text/plain, x-www-form-urlencoded 또는 json)를 같이 보내는 법을 알아보자.


환경

  • Linux
  • cURL


cURL

옵션을 사용해 데이터를 포함한 요청 보내기

  • -d 옵션을 사용
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'


사용 예제

text/plain

  • -d 옵션을 사용하고 POST 방식으로 text 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: text/plain" -d "raw text data" http://127.0.0.1:5000/test

application/x-www-form-urlencoded

  • -d 옵션을 사용하고 POST 방식으로 x-www-form-urlencoded 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2" http://127.0.0.1:5000/test

application/json

  • -d 옵션을 사용하고 POST 방식으로 JSON 형태의 자료를 보내는 예제
curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:5000/test -d '
{
   "test": "1234test",
   "child": {
      "child_1": "Hi",
      "cihld_2": "Hi"
   },
   "elements": [ "a", "b", "c" ]
}
'


참고자료