Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to make a POST request with cURL - DesignLinux

designlinux 0 Comments

cURL is a command-line utility for transferring data from or to a remote server using one of the supported protocols. It is installed by default on macOS and most Linux distributions.

cURL is used by developers for testing APIs , viewing response headers, and making HTTP requests.

In this article, we’re going to explain how to use cURL to make POST requests. The HTTP POST method is used to send data to the remote server.

Making a POST request #

The general form of the curl command for making a POST request is as follows:

curl -X POST [options] [URL]
The -X option specifies which HTTP request method will be used when communicating with the remote server.

The type of the request body is indicated by its Content-Type header.

Generally, a POST request is sent via an HTML form. The data send to the form is usually encoded in either multipart/form-data or application/x-www-form-urlencoded content type.

To create a POST request, use the -F option, followed by the field=value pair. The following example shows how to make a POST request to a form that has “name” and “email” fields:

curl -X POST -F 'name=linuxize' -F '[email protected]' https://example.com/contact.php

When the -F option is used, curl sends the data using the multipart/form-data Content-Type.

Another way to make a POST request is to use the -d option. This causes curl to send the data using the application/x-www-form-urlencoded Content-Type.

curl -X POST -d 'name=linuxize' -d '[email protected]' https://example.com/contact.php

If the -d option is used more than once you can merge the data using the & symbol:

curl -X POST -d 'name=linuxize&[email protected]' https://example.com/contact.php

Specifying the Content-Type #

To set a specific header or Content-Type use the -H option. The following command sets the POST request type to application/json and sends a JSON object:

curl -X POST -H "Content-Type: application/json" \    -d '{"name": "linuxize", "email": "[email protected]"}' \    https://example/contact

Uploading Files #

To POST a file with curl, simply add the @ symbol before the file location. The file can be an archive, image, document, etc.

curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload

Conclusion #

We’ve shown you how to use curl to make POST requests. For more information about curl, visit the Curl Documentation page.

If you have any questions or feedback, feel free to leave a comment.

curl terminal

Related

Tags: curl, terminal

How to Install TensorFlow on CentOS 8

Prev Post

How to Extract (Unzip) Tar Bz2 File

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 39
  • 605,789

DesignLinux.com © All rights reserved

Go to mobile version