[Linux](EN) Attach data(text/plain, x-www-form-urlencoded or json) when using cURL

Summarize how to attach data(text/plain, x-www-form-urlencoded or json) when using cURL


Environment and Prerequisite

  • Linux
  • cURL


cURL

Send request with data using option

  • Use -d option.
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'


Usage Example

text/plain

  • Example of sending text format data with using -d option and POST method.
curl -XPOST -H "Content-Type: text/plain" -d "raw text data" http://127.0.0.1:5000/test

application/x-www-form-urlencoded

  • Example of sending x-www-form-urlencoded format data with using -d option and POST method.
curl -XPOST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=value1&param2=value2" http://127.0.0.1:5000/test

application/json

  • Example of sending JSON format data with using -d option and POST method.
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" ]
}
'


Reference