It is possible to URL Encode data and query string in a cURL POST request using an option called --data-urlencode.
Example: cURL HTTP GET using URL Encoding% curl --get -v \
--data-urlencode 'product=$@mething useful' \
--data-urlencode 'quantity=22.5' \
--data-urlencode 'price=$55' \
https://example.org
Output:
* Trying 93.184.216.34:443...
* Connected to example.org (93.184.216.34) port 443 (#0)
* (304) (OUT), TLS handshake, Client hello (1):
...
* SSL connection using TLSv1.3 / AEAD-AES256-GCM-SHA384
* ALPN: server accepted h2
* Server certificate:
....
* h2h3 [:method: GET]
* h2h3 [:path: /?product=%24%40mething+useful&quantity=22.5&price=%2455]
....
* Using Stream ID: 1 (easy handle 0x143813400)
> GET /?product=%24%40mething+useful&quantity=22.5&price=%2455 HTTP/2
> Host: example.org
> user-agent: curl/7.85.0
> accept: */*
>
< HTTP/2 200
< age: 391534
< cache-control: max-age=604800
< content-type: text/html; charset=UTF-8
< date: Tue, 13 Dec 2022 10:24:19 GMT
< etag: "3147526947+ident"
< expires: Tue, 20 Dec 2022 10:24:19 GMT
< last-modified: Thu, 17 Oct 2019 07:18:26 GMT
< server: ECS (dcb/7F3B)
< vary: Accept-Encoding
< x-cache: HIT
< content-length: 1256
<
<!doctype html>
<html>
<head>
<title>Example Domain</title>
As you can see the query strings passing along with the cURL GET request has been URL encoded.
[:path: /?product=%24%40mething+useful&quantity=22.5&price=%2455]
In a similar way you can make use of --data-urlencode to perform a HTTP POST request.
Example: HTTP POST Request with URL Encoding in cURL
% curl \
--data-urlencode 'name=$@mething' \
--data-urlencode 'age=22' \
--data-urlencode 'dept=IT & Services' \
https://example.org
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to cURL,
- cURL Option to show HTTP Response Status Code
- URL Encode cURL HTTP GET\POST Data with Examples
- Capture cURL Request Output to a File
- Fix: Curl No Match Found Error
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer
- Send JSON Data as Payload using cURL HTTP POST Request
- cURL: Show Request and Response Headers
- Get cURL command from Chrome Inspect Network HTTP URL
- Save cURL Command Output to a external file
- cURL DELETE Request with Examples
- How to URL Encode Data for a cURL Request Parameters
- cURL Example using IP (IPv4 and IPv6) address and Port
- How to convert a Postman request to cURL Command
- [fix] command not found curl
- How to Pretty Print cURL JSON Output in Terminal
- How to follow redirects in cURL Command Request
- How to pass authorization header using cURL
- cURL -x Use of HTTP Proxy Server Examples
- Downloading Google Chrome using cURL Command
- How to get cURL Command to run in verbose mode?
- List of 28 Protocols supported by cURL with Examples
- Hide cURL Outputs and Errors on Terminal
- cURL -d Option with Examples
- How to show SSL Certificate details using cURL Command
- Perform Basic Authentication using cURL with Examples
More Posts:
- Fix: Microsoft OneDrive We are currently experiencing technical difficulties - Microsoft
- Error running 'app': No target device found. - Android-Studio
- How to turn off 5G Network on iPhone - iOS
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree - Python
- Fix Microsoft Office 365 error code 135011 - Your organization has disabled this device - Microsoft
- Open .bash_profile File in TextEdit using Terminal - MacOS
- Run Scripting Language (JavaScript, Python, Groovy, Ruby) in Java - Java
- [git] fatal: clean.requireForce defaults to true nor -f given; refusing to clean - Git