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,
- 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:
- [Eclipse] Locate Preferences in macOS - MacOS
- How to Get the Relative Path of a file in Python Program - Python
- Pdf Text to Speech option in Mac OS X Preview App - Mac-OS-X
- Setting Java_Home Environment variable on Windows Operating System - Java
- pip/pip3 ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied - PIP
- Fix Microsoft Store Error Code: 0x80072EFD - Microsoft
- JavaScript: Check if variable is a number - JavaScript
- Fix: type argument is not within bounds of type-variable T - Java