Linux make post request

How to Perform a POST Request Using Curl

Curl command is referred to as a “Client URL” and universally used command line utility available for Windows, Mac, and Linux operating systems. This command uses different protocols such as HTTPS, FTP, SMP, and so on to download images and files from the URL, send data to the server, access data from the URL, and get the server content. Sending data to the server is done through Post request while getting data from the server is done through Get request.

This article will demonstrate how to perform a POST request using the curl command.

How to Perform a POST Request Using curl Command?

In a POST request, the “POST” is an HTTP request method utilized to send requests or data to the server through HTTPS or HTTP protocol. The “curl” command helps us to send a POST request to the server through the URL.

Prerequisites: Install Curl Command

To send a POST request to a server through the Client URL, first, install the “curl” command on Ubuntu using the “apt install curl” with “sudo” rights:

Send Simple Post Request Using Curl

In order to send simple POST requests through the curl command, utilize the “curl -X POST ” command:

Here, “-X” is used to select the HTTP request method such as the “POST” method.

Send Data in Post Request Using Curl

The POST request method is particularly used to send or transfer data to a server or specified URL. To send data to the server, utilize the following options:

    • -d” option specifies the data that needs to be transferred.
    • -H” defines the header content type or data type that is transferred through a POST request.
    • -X” is utilized to choose and use the POST request method with URL:

    Write Response of Post Request in Output File

    The output or response of a POST request can be saved or overwritten to a file using the “-o” option. For instance, we have saved the response in the “test.html” file:

    curl -d ‘<"e-mail":"rafia@gmail.com", "password":"password123">‘ -H «Content-Type: multipart/form-data» -X POST https: // reqbin.com / echo / post / json -o test.html

    After that, execute the file and check the output or response of POST request:

    Send File in Post Request Using Curl

    POST requests not only send data but also can exchange files as well. In order to send the entire file in POST request, first, create the file using the “touch” command and save the content into the file.

    For instance, we have created “file.txt”:

    Next, utilize the “curl” command along with the POST request as mentioned in the below command:

    Here, “–form” option is used to send the form data in the POST request:

    That is all about sending a POST request using the “curl” command.

    Conclusion

    To perform a POST request using the “curl” command, first, install the “curl” on your system through the “sudo apt install curl” command. After that, send the POST request in the “curl” command using the “curl -X POST ” command along with options such as “-d”, “-H”, and “–form”. This post has illustrated how to perform a POST request using the curl command.

    About the author

    Rafia Zafar

    I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.

    Источник

    How to make a POST request with cURL

    How to make a POST request with cURL - TechvBlogs

    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.

    Suresh Ramani - Author - TechvBlogs

    Suresh Ramani

    SSD VPS Servers, Cloud Servers and Cloud Hosting - Vultr.com

    What is cURL?

    Curl is a command-line utility that allows users to create network requests. Curl is accessible on Windows, Linux, and Mac, making it the go-to choice for developers across all platforms.

    A cURL is computer software and command-line tool used to make requests for different protocols. But the most popular usage with the curl command is making HTTP post requests. Even the curl command-line tool is created for the Linux operating systems it is cross-platform and can be used for Windows, MacOSX, BSD, etc. In this tutorial, we will learn how to make different HTTP POST requests by using curl.

    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:

    The -X the 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 sent to the form is usually encoded in either multipart/form-data or application/x-www-form-urlencoded content type.

    Make a simple POST Request

    We start with a simple example where we make a POST request to the specified URL. The -X option is used to specify the request type which is POST and we also provide the URL.

    curl -X POST http://example.com

    If the -X POST is not specified the GET method of the HTTP protocol is used by default.

    Send Additional Fields with POST Request

    The POST request can be used to send some data to the remote URL with the POST request. The data can be specified via the command line by using the -d option and data like below.

    curl -d "firstname=John&lastname=Andrew" -X POST http://example.com

    The provided data is separated with the & sign and generally structured as name=value. Using the -d option also adds some implicit HTTP headers like Content-Type etc.

    Specifying the Content-Type in the POST request

    The -H a flag can be used to send a specific data type or header with a curl. The following command sends a JSON object with the request.

    curl -d '' -H 'Content-Type: application/json' https://example.com

    Specify Cookie with POST Request

    Some web applications use authentication and authorization which rely on cookies. The curl command can be used with a cookie to access restricted resources by authenticating requests. The -b or –cookie option can be used to specify the cookie data.

    curl --cookie "sadad1321saweqe" -X POST http://example.com

    Sending a file using curl

    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/Downloads/profile.jpg' http://example.com/upload

    Send JSON Data using curl

    One of the most popular use-cases for the curl command is using the JSON format for communication, especially for requests. In the following example, we send JSON data to the server.

    curl -d '' -H "Content-Type: application/json" -X POST "http://example.com/data"

    Alternatively, the JSON data can be located in a file and this file JSON data can be sent to the server like below.

    curl -d "@mydata.json" -X POST "http://example.com/data"

    Conclusion

    This article shows the importance of the Curl command in Linux and discusses the usage of curl post requests on Linux. I clarified how to make POST requests using the CURL command.

    Thank you for reading this article.

    If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

    If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

    Источник

    How to make a POST request with curl

    The curl is a command-line utility that enables the user to transfer data using any standard protocol. It is functional on all key operating systems including Linux. The curl command is mainly used to test the APIs (Application programming interface) by sending the post request and comes pre-installed on many Linux distributions. However, we need to install the curl command manually in Ubuntu 20.04 (LTS) long-term support.

    The Ubuntu 20.04 LTS is being used for the demonstration.

    Install curl on Ubuntu 20.04

    Type the below-given command to install curl in Ubuntu 20.04:

    Note: You can skip this step if the curl command is already installed on your system.

    Once the curl command is installed, verify the installed version using the command:

    Make POST request using curl

    We will use the HTTP post method to send data to the remote server.

    Following is the simple form of the curl command to send a post request:

    $ curl -X POST [options] [the URL of server]

    The -X option is used to describe the HTTP method. The curl command employs the Get method as the default HTTP method. However, to make a POST request, we need to specify POST with the -X option.

    A general post request can be made as follows:

    $ curl –X POST https://example.com/

    Send additional fields with the POST request

    Normally, a post request is made by an HTML form.

    I have created a demo API that accepts the user name and age. Let’s make a post request and send the name and age values. The –d option is used with the curl command to send the additional field’s data to the remote server.

    $ curl -X POST -d 'name=kamran&age=24' https://curlliuxexample.000webhostapp.com/index.php

    Instead of the –d option, we can also use the –F option to send the additional fields with the post request as follows:

    curl -X POST -F 'name=kamran' -F 'age=24' https://curlliuxexample.000webhostapp.com/index.php

    link cmd

    While using the –F option, we cannot merge the data with & operator. The –d option uses the application/x-www-form-urlencoded Content-Type while send the additional field’s data to the server, whereas the-F option uses the multipart/form-data Content-Type.

    Specify the Header or Content-type in POST request

    The Header or Content-type can be specified in a POST request using -H option. Let’s set the Content-Type to application/json and sends the JSON data using the command:

    $ curl -X POST -H "Content-Type: application/json" -d '' https://example.com

    Upload the file(s) with the curl command

    The files can be uploaded using the curl command. To upload a file with the curl command, just add ‘@’ before the location of the file:

    $ curl -X POST -F '[email protected]/home/Downloads/mypic.jpg' http://example.com 

    In the afore-mentioned command, I am uploading an image file using the curl command.

    Conclusion

    This post describes the usage of the curl command. Using the curl command, we can send the additional field data and upload the files to the server.

    TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY

    Источник

    Читайте также:  Site.local Тестовая страница
Оцените статью
Adblock
detector