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!
More Posts related to cURL,
- Hide cURL Outputs and Errors on Terminal
- cURL DELETE Request with Examples
- Downloading Google Chrome using cURL Command
- List of 28 Protocols supported by cURL with Examples
- Get the Size of HTTP Response using cURL Command (Content Length)
- How to convert a Postman request to cURL Command
- How to URL Encode Data for a cURL Request Parameters
- Get cURL command from Chrome Inspect Network HTTP URL
- Fetch only content-type using cURL Command
- cURL -d Option with Examples
- cURL Option to show HTTP Response Status Code
- URL Encode cURL HTTP GET\POST Data with Examples
- How to get cURL Command to run in verbose mode?
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer
- [fix] command not found curl
- How to Pretty Print cURL JSON Output in Terminal
- Capture cURL Request Output to a File
- List of common cURL Error Codes
- How to pass authorization header using cURL
- Perform Basic Authentication using cURL with Examples
- Trace using cURL Command Example
- Efficient way to perform HTTP cURL GET/POST Requests with Payload
- Save cURL Command Output to a external file
- cURL Example using IP (IPv4 and IPv6) address and Port
- How to follow redirects in cURL Command Request
More Posts:
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error - Git
- Syntax error, parameterized types are only available if source level is 1.5 or greater [Java] - Eclipse
- How to open a new tab in Notepad++ - NotepadPlusPlus
- Convert GMT/UTC timezone in Excel to EST, CST, MST, PST, AST and HST - Microsoft
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- Merge multiple zip files without unzipping (extracting) - HowTos
- Only Chessboard using HTML and CSS Code Example - CSS
- How to switch to Powershell on Mac Terminal - Powershell