URL Encode cURL HTTP GET\POST Data with Examples


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]

cURL URL Encoding example HTTP GET

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



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap