Advertisement
miguelcl

CURL_JSON_EXAMPLES

Oct 1st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.67 KB | None | 0 0
  1. Common Options
  2. -#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.
  3.  
  4. -b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).
  5.  
  6. -c, --cookie-jar <file name> File to save response cookies to.
  7.  
  8. -d, --data <data> Send specified data in POST request. Details provided below.
  9.  
  10. -f, --fail Fail silently (don't output HTML error form if returned).
  11.  
  12. -F, --form <name=content> Submit form data.
  13.  
  14. -H, --header <header> Headers to supply with request.
  15.  
  16. -i, --include Include HTTP headers in the output.
  17.  
  18. -I, --head Fetch headers only.
  19.  
  20. -k, --insecure Allow insecure connections to succeed.
  21.  
  22. -L, --location Follow redirects.
  23.  
  24. -o, --output <file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.
  25.  
  26. -O, --remote-name Write output to file named like the remote file (only writes to current directory).
  27.  
  28. -s, --silent Silent (quiet) mode. Use with -S to force it to show errors.
  29.  
  30. -v, --verbose Provide more information (useful for debugging).
  31.  
  32. -w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w "\n" (can add to ~/.curlrc).
  33.  
  34. -X, --request The request method to use.
  35.  
  36. POST
  37. When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:
  38.  
  39. application/json
  40. application/x-www-form-urlencoded
  41. Many APIs will accept both formats, so if you're using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because
  42.  
  43. the json format requires a bunch of extra quoting
  44. curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set
  45. This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests.
  46.  
  47. curl usage
  48. For sending data with POST and PUT requests, these are common curl options:
  49.  
  50. request type
  51.  
  52. -X POST
  53. -X PUT
  54. content type header
  55.  
  56. -H "Content-Type: application/x-www-form-urlencoded"
  57.  
  58. -H "Content-Type: application/json"
  59.  
  60. data
  61.  
  62. form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt
  63. json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json
  64. Examples
  65. POST application/x-www-form-urlencoded
  66. application/x-www-form-urlencoded is the default:
  67.  
  68. curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data
  69. explicit:
  70.  
  71. curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
  72. with a data file
  73.  
  74. curl -d "@data.txt" -X POST http://localhost:3000/data
  75. POST application/json
  76. curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
  77. with a data file
  78.  
  79. curl -d "@data.json" -X POST http://localhost:3000/data
  80.  data.json
  81. {
  82.   "key1":"value1",
  83.   "key2":"value2"
  84. }
  85.  data.txt
  86. param1=value1&param2=value2
  87. #package.json
  88. {
  89.   "name": "postdemo",
  90.   "version": "1.0.0",
  91.   "scripts": {
  92.     "start": "node server.js"
  93.   },
  94.   "dependencies": {
  95.     "body-parser": "^1.15.0",
  96.     "express": "^4.13.4"
  97.   }
  98. }
  99. #server.js
  100. var app = require('express')();
  101. var bodyParser = require('body-parser');
  102.  
  103. app.use(bodyParser.json()); // for parsing application/json
  104. app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  105.  
  106. app.post('/data', function (req, res) {
  107.   console.log(req.body);
  108.   res.end();
  109. });
  110.  
  111. app.listen(3000);
  112.  
  113.  
  114. https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement