📜  curl post - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:10.771000             🧑  作者: Mango

CURL POST - Shell/Bash

What is CURL?

CURL is a command-line tool that can be used for transferring data to and from servers. It supports various protocols like HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, LDAP, LDAPS, DICT, TELNET, and many more.

How to use CURL to make a POST request?

To make a POST request using CURL, you need to specify the request method, URL, and the data to be sent in the request body. You can also specify headers and other options using the command-line arguments.

Here is an example CURL command to make a POST request:

curl -X POST \
  -H 'Content-Type: application/json' \
  -d '{"name": "John Doe", "email": "johndoe@example.com"}' \
  https://example.com/api/users
  • -X POST: Specifies the request method to be POST.
  • -H 'Content-Type: application/json': Specifies the content type of the request body as JSON.
  • -d '{"name": "John Doe", "email": "johndoe@example.com"}': Specifies the data to be sent in the request body as a JSON object.
  • https://example.com/api/users: Specifies the URL of the API to which the request will be sent.
Tips and Tricks for using CURL
  • To send data in a URL-encoded format, use the -d option with the data in the key=value format.
  • To send data as a file, use the -d option followed by the file name with @ prefix.
  • To send data in a multipart format (e.g. for uploading files), use the -F option with the data in the key=@/path/to/file format.
  • To specify cookies in the request, use the -b option with the cookie data in the name=value format.
  • To save the response in a file, use the -o option followed by the file name.
Conclusion

CURL is a powerful tool that can be used for making HTTP requests in the command line. With its various options and features, you can easily interact with APIs and servers using just a few commands.