curl – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sun, 01 Jan 2023 06:41:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 cURL – How to display request headers and response headers https://tecadmin.net/curl-display-request-headers-and-response-headers/ https://tecadmin.net/curl-display-request-headers-and-response-headers/#respond Sat, 10 Sep 2022 10:19:11 +0000 https://tecadmin.net/?p=31577 Request Header and Response Header are both a part of the HTTP protocol, which is the standard used for communication between web browsers and web servers. The Request Header is sent by the browser as part of an HTTP request, and it contains information such as the type of request, the URL of the requested [...]

The post cURL – How to display request headers and response headers appeared first on TecAdmin.

]]>
Request Header and Response Header are both a part of the HTTP protocol, which is the standard used for communication between web browsers and web servers. The Request Header is sent by the browser as part of an HTTP request, and it contains information such as the type of request, the URL of the requested page, and any authentication credentials. The Response Header is sent by the server in response to the request, and it contains information such as the status code of the response, the content type of the page, and any authentication credentials.

Together, the Request and Response Headers help to ensure that data is sent securely and accurately between the browser and the server. Request and Response Headers are essential for web developers as they provide important information for debugging and troubleshooting. If you’re interested in learning more about Request and Response Headers, a good place to start is by reading up on the HTTP protocol.

cURL is a command line utility used to transmit data over different-2 protocols. It is a quick tool for developers to view the request header and response header values of a website.

1. cURL – Get Request Headers

Use --versbose or -v option with the curl command to fetch the request header and response header values as following:

curl --verbose google.com 
how to get request and response header with curl
cURL – get the request header and response header values

2. cURL – Get Response Headers

You can also use curl to fetch the response header values only. Use -I option to get the response header values.

curl -I google.com 
Output:
HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Sat, 10 Sep 2022 09:25:56 GMT Expires: Mon, 10 Oct 2022 09:25:56 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN

3. cURL – Get Custom Header Values

Sometimes you may need to fetch the specific header value. That is helpful for scripting and many other tasks. Use the grep command to filter specific values from complete header values. The -F is used to search fixed string and -i is used for case-sensitive search.

curl -I google.com | grep -Fi "Content-Type" 
Output:
Content-Type: text/html; charset=UTF-8

Wrap Up

cURL is a command line utility that is helpful for multiple tasks. We can also use curl to request a server for the details. This tutorial helped you to get the request header and response header values using the curl command line.

The post cURL – How to display request headers and response headers appeared first on TecAdmin.

]]>
https://tecadmin.net/curl-display-request-headers-and-response-headers/feed/ 0
curl command in Linux with Examples https://tecadmin.net/linux-curl-command-with-examples/ https://tecadmin.net/linux-curl-command-with-examples/#respond Thu, 03 Dec 2020 18:41:55 +0000 https://tecadmin.net/?p=23757 curl is an command line tool for transferring data between two servers. Other than downloading files curl also used to performs multiple tasks by the applications, services etc. Curl supported a verity of protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, [...]

The post curl command in Linux with Examples appeared first on TecAdmin.

]]>
curl is an command line tool for transferring data between two servers. Other than downloading files curl also used to performs multiple tasks by the applications, services etc. Curl supported a verity of protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP) for file transferring.

The curl is powered by the libcurl for all the transfer-related tasks on system.

  • Syntax:
    curl [options] [URL...]
    

    Example: Open a terminal on your system and type:

    curl https://tecadmin.net
    
  • You will see the website content on terminal. This is the most basic uses of curl command line.

URL Syntax

The URL syntax is completely protocol-dependent with cURL. Before reading more about parameters or example, you must know the URL formats, you can use with curl.

  • Use braces and quotes to define multiple URLs in single. Here braces expands to multiple urls. For example:
      "http://www.{one,two,three}.com"
    

    Becomes, http://www.one.com, http://www.two.com and http://www.three.com.

  • You can also define a range by using [] as in:
      "ftp://ftp.example.com/file[1-100].txt"
      "http://ftp.example.com/file[a-z].txt"
    
  • You can also specify to use every N’th letter or number from a defined range.
      "ftp://ftp.example.com/file[1-100:5].txt"
      "http://ftp.example.com/file[a-z:2].txt"
    

    Here the first url will refer to every 5’th file and second url with refer to every second letter.

CURL Command Options

curl command comes with large number of command line options. Which provides it great flexibility to perform various tasks. Here we will describe you some frequently used command options with curl command.

  • -s or --silent – While using this option, the command runs silently in background. No progress will be displayed on screen. Only the command result will be displayed.
    curl -s http://www.example.com 
    
  • -O – The capital letter “O” is used to download a file using curl command. The filename will remain same on local system as on remote.
    curl -O http://www.example.com/backup.zip 
    
  • -o or --output FILE – Use this option to write all data to file instead of displaying at standard output.
    curl -o file.txt http://www.example.com 
    

    While downloading a file, use this option to save file on local machine with provided name.

    curl -o local.zip http://www.example.com/remote.zip 
    
  • -I or --head – Use this option to view the document information only. This will not download the content or file from server.

    This is also useful to view header only details for a domain.

    curl -I http://www.example.com 
    
  • -u or --user – Use this option to send authentication details with curl request. It is useful to download files from authenticated ftp server or web servers.
    curl -u "username:password" -O ftp://ftp.example.com/remote.zip 
    
  • -T – curl also allows you to upload a file to remote ftp server. To upload a file use -T option followed by the local file name. If the remote server required authentication, make sure to provide authentication details with “-u” option.
    curl -u ftpuser:ftppassword -T localfile.zip ftp://ftp.example.com/files/ 
    
  • -x or -–proxy – You can route your curl request via a proxy server. You can define proxy server with -x option.
    curl -x some.proxy.com:3128 http://www.example.com 
    

Similar tutorials:

Conclusion

In this tutorial, you have learned about curl command line options with examples. For more command line options view curl man pages (man curl) or use curl --help command.

The post curl command in Linux with Examples appeared first on TecAdmin.

]]>
https://tecadmin.net/linux-curl-command-with-examples/feed/ 0
How to Use Curl Command with Proxy on Linux https://tecadmin.net/curl-command-with-proxy/ https://tecadmin.net/curl-command-with-proxy/#respond Wed, 11 Sep 2019 08:55:39 +0000 https://tecadmin.net/?p=19463 Curl is a utility used for data transfer in command lines or scripts. In this tutorial, you will learn how to use the curl command to connect via a proxy server on the Linux system. Using Environment Variable In a very simple word, you can simply set the following environment variables on your system. The [...]

The post How to Use Curl Command with Proxy on Linux appeared first on TecAdmin.

]]>
Curl is a utility used for data transfer in command lines or scripts. In this tutorial, you will learn how to use the curl command to connect via a proxy server on the Linux system.

Using Environment Variable

In a very simple word, you can simply set the following environment variables on your system. The curl command will automatically use these variables as a proxy. You can export those variables in your shell, like:

Without Authentication 

export http_proxy="http://proxy.server:port"
export https_proxy="https://proxy.server:port"

With Authentication 

export http_proxy="http://username:password@proxy.server:port"
export https_proxy="https://username:password@proxy.server:port"

After exporting the environment variables, the curl command will automatically use the above proxy during a remote connection.

curl http://example.com

Once your work is done, you can simply unset these environment variables like:

unset http_proxy
unset https_proxy

Without Environment Variable

Instead of setting environment variables, you can simply pass the proxy details to curl command as a command-line parameter.

curl -x "http://username:password@proxy.server:port" http://example.com

The post How to Use Curl Command with Proxy on Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/curl-command-with-proxy/feed/ 0
How to Force Use TLS 1.2 with cURL PHP https://tecadmin.net/use-tls12-with-curl-php/ https://tecadmin.net/use-tls12-with-curl-php/#respond Sun, 11 Aug 2019 05:12:08 +0000 https://tecadmin.net/?p=19072 Most of the Web/API services providers are shifting their environments to TLS 1.2 or greater. So to consume their services via PHP applications, you also need to force your application to use TLS 1.2 during making a connection. This tutorial will help you, how to use TLS 1.2 with PHP cURL. Using TLS 1.2 with [...]

The post How to Force Use TLS 1.2 with cURL PHP appeared first on TecAdmin.

]]>
Most of the Web/API services providers are shifting their environments to TLS 1.2 or greater. So to consume their services via PHP applications, you also need to force your application to use TLS 1.2 during making a connection. This tutorial will help you, how to use TLS 1.2 with PHP cURL.

Using TLS 1.2 with PHP CURL Forcefully

You can add the following code to your curl requests to use TLS 1.2. Use 6 as the value of CURLOPT_SSLVERSION forces cURL to use TLS 1.2.

Below is the sample code to force use tls 1.2 with php curl:

curl_setopt ($ch, CURLOPT_SSLVERSION, 6);

For the example, I am using a sample script from our another articlesubmitting JSON data with cURL and PHP. In that script, we will add code to forece use of tls 1.2.

Below is the sample script:

<?php
// A sample PHP Script to POST data using cURL
// Data in JSON format
 
$data = array(
    'username' => 'tecadmin',
    'password' => '012345678'
);
 
$payload = json_encode($data);

$ch = curl_init('https://api.example.com/api/1.0/user/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

curl_setopt ($ch, CURLOPT_SSLVERSION, 6);  //Force requsts to use TLS 1.2
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload))
);
 
$result = curl_exec($ch);
curl_close($ch);
?>

You can execute above script in webbrowser or from the command line interface.

Conclusion

In this tutorial, you have learned to use tls 1.2 with PHP/cURL forcefully.

The post How to Force Use TLS 1.2 with cURL PHP appeared first on TecAdmin.

]]>
https://tecadmin.net/use-tls12-with-curl-php/feed/ 0
How To POST JSON Data with PHP cURL https://tecadmin.net/post-json-data-php-curl/ https://tecadmin.net/post-json-data-php-curl/#comments Fri, 09 Mar 2018 16:33:46 +0000 https://tecadmin.net/?p=13298 The PHP cURL is a library used for making HTTP requests. In order to use PHP cURL, you must have installed and enabled libcurl module for PHP on your system. In this tutorial, you will learn how to POST JSON data with PHP cURL requests. Basically, there are 4 steps involved to complete a cURL [...]

The post How To POST JSON Data with PHP cURL appeared first on TecAdmin.

]]>
The PHP cURL is a library used for making HTTP requests. In order to use PHP cURL, you must have installed and enabled libcurl module for PHP on your system. In this tutorial, you will learn how to POST JSON data with PHP cURL requests. Basically, there are 4 steps involved to complete a cURL request using PHP.

  • curl_init — The first step is to initializes a new session of cURL and return a cURL handle to other functions.
  • curl_setopt — The second step is to set options for a cURL session handle. All these settings are very well explained at curl_setopt().
  • curl_exec — In third step it perform a cURL session based on above options set.
  • curl_close — The last step is to close a cURL session initialize by curl_init() and free all resources. Also deleted the cURL handle.

Let’s use the below sample code to create a POST request with PHP cURL.

<?php
// A sample PHP Script to POST data using cURL
// Data in JSON format

$data = array(
    'username' => 'tecadmin',
    'password' => '012345678'
);

$payload = json_encode($data);

// Prepare new cURL resource
$ch = curl_init('https://api.example.com/api/1.0/user/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set HTTP Header for POST request 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload))
);

// Submit the POST request
$result = curl_exec($ch);

// Close cURL session handle
curl_close($ch);

?>

The main thing is that the request must be a POST request with properly json-encoded data in the body. The headers must properly describe the post body.

The post How To POST JSON Data with PHP cURL appeared first on TecAdmin.

]]>
https://tecadmin.net/post-json-data-php-curl/feed/ 12
Making a POST Request with a JSON Payload using Curl https://tecadmin.net/post-json-data-with-curl-command/ https://tecadmin.net/post-json-data-with-curl-command/#comments Thu, 06 Jul 2017 10:08:44 +0000 https://tecadmin.net/?p=13296 Question- How to send a POST Request with a JSON Body using the Curl Command Line? The `curl` command line utility is a powerful tool for making HTTP requests. It can be used to send a variety of different HTTP requests, including POST requests with a JSON body. Here’s how you can use curl to [...]

The post Making a POST Request with a JSON Payload using Curl appeared first on TecAdmin.

]]>
Question- How to send a POST Request with a JSON Body using the Curl Command Line?

The `curl` command line utility is a powerful tool for making HTTP requests. It can be used to send a variety of different HTTP requests, including POST requests with a JSON body. Here’s how you can use curl to send a POST request with a JSON body:

  • Create a JSON file
  • Create a JSON file that contains the data you want to send in the request body. For example, let’s say you have a file named `data.json` with the following contents:

    {
      "name": "John Doe",
      "age": 25
    }

  • Curl POST Data
  • Use the curl command to send a POST request with the JSON data. The `-X` option specifies the request method (in this case, POST), and the `-H` option adds an HTTP header (in this case, `Content-Type: application/json` to specify that the request body is in JSON format). The -d option specifies the request body, and the `@` symbol tells curl to read the data from a file.

    Here’s the command to send the POST request with the JSON data:

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

  • Curl POST Data with Response Header
  • If the request is successful, the server will return a response. You can use the `-i` option to include the response headers in the output, or the `-o` option to save the response to a file.

    Here’s an example of using the `-i` option to print the response headers:

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

    And here’s an example of using the `-o` option to save the response to a file:

    curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com/endpoint -o response.txt 
    

That’s all there is to it! With these simple commands, you can use `curl` to send a POST request with a JSON body to a server.

Keep in mind that the JSON data in the request body must be properly formatted and valid, or the request may fail. You can use a tool like JSONLint (https://jsonlint.com/) to validate your JSON data before sending it in the request.

I hope this tutorial has been helpful in showing you how to use `curl` to send a POST request with a JSON body. If you have any questions or need further assistance, don’t hesitate to ask.

The post Making a POST Request with a JSON Payload using Curl appeared first on TecAdmin.

]]>
https://tecadmin.net/post-json-data-with-curl-command/feed/ 5
How to Download Files with cURL (5 Examples) https://tecadmin.net/curl-files-download-examples/ https://tecadmin.net/curl-files-download-examples/#respond Thu, 01 Oct 2015 09:56:47 +0000 https://tecadmin.net/?p=8666 cURL is an open source command line tool and library for transferring data from remote systems. cURL support wide range of protocols like FILE, FTP, FTPS,HTTP, HTTPS, SCP, SFTP and many more. This article will help you to how to download remote files using cURL command line. 1. Download Single File Use following command to [...]

The post How to Download Files with cURL (5 Examples) appeared first on TecAdmin.

]]>
cURL is an open source command line tool and library for transferring data from remote systems. cURL support wide range of protocols like FILE, FTP, FTPS,HTTP, HTTPS, SCP, SFTP and many more. This article will help you to how to download remote files using cURL command line.

1. Download Single File

Use following command to download a single file from remote server using HTTP protocol. The following example will download latest.tar.gz from remote server and save in current directory with same name.

curl -O http://wordpress.org/latest.tar.gz


% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   178  100   178    0    0    239      0 --:--:-- --:--:-- --:--:--   239

2. Download File and Save with Other Name

Use following command to download a single file from remote server and save at specified location with specified name on local disk. The following example will download latest.tar.gz from remote server and save in /tmp/ directory with name wp.tar.gz.

curl -o /tmp/wp.tar.gz  http://wordpress.org/latest.tar.gz


% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   178  100   178    0    0    212      0 --:--:-- --:--:-- --:--:--   212

3. Download Multiple Files

Use following command to download files from multiple files from multiple remote servers using HTTP protocol. The following example will download latest.tar.gz and latest.zip from remote servers and save in current directory with same names.

curl -O http://wordpress.org/latest.tar.gz -O http://wordpress.org/latest.zip


% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   178  100   178    0    0    202      0 --:--:-- --:--:-- --:--:--   202
100   178  100   178    0    0    387      0 --:--:-- --:--:-- --:--:--   387

4. Download File with Authentication

If remote files are behind authentication and required username and password to download files. Use following examples to download files.

Download files from ftp server with ftp login credentials

curl -u ftpuser:ftppasswd -O ftp://ftp.example.com/file.zip

Download files from http server with http login credentials.

curl -u username:password -O http://wordpress.org/latest.tar.gz

5. Download Files Behind Proxy Server

If your connection required proxy server to download remote files. Use following examples to download files through proxy server. Use -x following by proxy_server:port .

curl -x 192.168.0.100:3128 -L -O  http://wordpress.org/latest.tar.gz 


% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   178  100   178    0    0    435      0 --:--:-- --:--:-- --:--:--   435
100 6373k  100 6373k    0    0   116k      0  0:00:54  0:00:54 --:--:--  102k

The post How to Download Files with cURL (5 Examples) appeared first on TecAdmin.

]]>
https://tecadmin.net/curl-files-download-examples/feed/ 0
5 Curl Commands to download Files https://tecadmin.net/5-curl-commands-to-download-files/ https://tecadmin.net/5-curl-commands-to-download-files/#comments Mon, 16 Feb 2015 03:57:50 +0000 https://tecadmin.net/?p=7362 Curl command file utility supports for downloading and uploading files. Curl is useful for many works with system administration, web development for calling web services, etc. In this tutorial, we are providing 5 frequently used curl commands to download files from remote servers. 1. Simple curl command to download file To download a file using [...]

The post 5 Curl Commands to download Files appeared first on TecAdmin.

]]>
Curl command file utility supports for downloading and uploading files. Curl is useful for many works with system administration, web development for calling web services, etc. In this tutorial, we are providing 5 frequently used curl commands to download files from remote servers.

1. Simple curl command to download file

To download a file using curl use the following syntax. -O is used for saving files on the local system with the same name on the remote system.

curl -O http://example.com/download/myfile.zip 

2. Curl download file and save with different name

You can also save the downloaded file with a different name on the local machine. Use -o followed by the new file name to download and save files with a different name.

curl -o localname.zip http://example.com/download/myfile.zip 

3. Curl to Download Multiple Files

Curl also provides an option to download multiple files simultaneously. To download multiple files at once using the following syntax. All files will be saved with original file names.

curl -O http://example.com/myfile.zip -O http://example.com/myfile2.zip 

4. Use Login Credential with Curl Download

Some of the remote resources required authentication to download any file. Curl also allows you to provide authentication details to authorize requests. You can pass login credentials using -u option for HTTP for FTP requests, like:

curl -u user:password -O http://example.com/myfile.zip 
curl -u ftpuser:ftppassword -O ftp://ftp.example.com/myfile.zip 

5. Curl download file via proxy

If the server file is only available through a proxy server, or you want to use a proxy for downloading files, Use -x followed by a proxy address and port to download the file via a proxy server.

curl -x "my.proxy.com:3128" -O http://example.net/myfile.zip 

Conclusion

Curl is a useful utility to create GET, POST, HEADER, and many other requests to remote servers. Even it is helpful for downloading remote files quickly like wget. This tutorial 5 examples of the curl commands for downloading files from a remote server.

The post 5 Curl Commands to download Files appeared first on TecAdmin.

]]>
https://tecadmin.net/5-curl-commands-to-download-files/feed/ 5