If you are wondering how to URL encode the data that you pass as query string or parameters for an HTTP request using cURL, you need to take a look at the --data-urlencode option
Let's look at the man page.
% man curl
....
....
--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL encoding.
To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification. The <data> part
can be passed to curl using one of the following syntaxes:
content
This will make curl URL-encode the content and pass that on. Just be careful so that the content does not contain any = or @
symbols, as that, will then make the syntax match one of the other cases below!
=content
This will make curl URL-encode the content and pass that on. The preceding = symbol is not included in the data.
name=content
This will make curl URL-encode the content part and pass that on. Note that the name part is expected to be URL-encoded
already.
@filename
This will make curl load data from the given file (including any newlines), URL-encode that data and pass it on in the POST.
name@filename
This will make curl load data from the given file (including any newlines), URL-encode that data and pass it on in the POST.
The name part gets an equal sign appended, resulting in name=urlencoded-file-content. Note that the name is expected to be
URL-encoded already.
...
...
Examples:
curl --data-urlencode name=val https://example.com
curl --data-urlencode =encodethis https://example.com
curl --data-urlencode name@file https://example.com
curl --data-urlencode @fileonly https://example.com
Example:
curl -X POST \
--data-urlencode "user=101" \
--data-urlencode "date=2023-06-23" \
--data-urlencode "location=New York" https://code2care.org/v1/api/data/
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:
- How to find current logged-in user in Linux Terminal - Linux
- bash: ls command to see list files in current directory all subdirectories - Bash
- Run only a Single Unit Test using Gradle - Gradle
- [Fix] Steam Friends Network Unreachable Error - HowTos
- How to make use of SQLite Module in Python? - Python
- How to turn off Dark Mode in Excel App for Mac - Windows
- How to show console in Eclipse IDE - Eclipse
- Fix Power BI 404 not found error when connecting SharePoint Online List as Data Source - SharePoint