1libcurl(3) libcurl libcurl(3)
2
3
4
6 libcurl-url - URL interface overview
7
9 The URL interface provides functions for parsing and generating URLs.
10
12 You still only include <curl/curl.h> in your code.
13
15 Create a handle that holds URL info and resources with curl_url(3):
16 CURLU *h = curl_url();
17
19 When done with it, clean it up with curl_url_cleanup(3)
20 curl_url_cleanup(h);
21
23 When you need a copy of a handle, just duplicate it with
24 curl_url_dup(3):
25 CURLU *nh = curl_url_dup(h);
26
28 By setting a URL to the handle with curl_url_set(3), the URL is parsed
29 and stored in the handle. If the URL is not syntactically correct it
30 will return an error instead.
31 rc = curl_url_set(h, CURLUPART_URL,
32 "https://example.com:449/foo/bar?name=moo", 0);
33
34 The zero in the fourth argument is a bitmask for changing specific fea‐
35 tures.
36
37 If successful, this stores the URL in its individual parts within the
38 handle.
39
41 When a handle already contains info about a URL, setting a relative URL
42 will make it "redirect" to adapt to it.
43 rc = curl_url_set(h, CURLUPART_URL, "../test?another", 0);
44
46 The CURLU handle represents a URL and you can easily extract that with
47 curl_url_get(3):
48 char *url;
49 rc = curl_url_get(h, CURLUPART_URL, &url, 0);
50 curl_free(url);
51 The zero in the fourth argument is a bitmask for changing specific fea‐
52 tures.
53
55 When a URL has been parsed or parts have been set, you can extract
56 those pieces from the handle at any time.
57
58 rc = curl_url_get(h, CURLUPART_FRAGMENT, &fragment, 0);
59 rc = curl_url_get(h, CURLUPART_HOST, &host, 0);
60 rc = curl_url_get(h, CURLUPART_PASSWORD, &password, 0);
61 rc = curl_url_get(h, CURLUPART_PATH, &path, 0);
62 rc = curl_url_get(h, CURLUPART_PORT, &port, 0);
63 rc = curl_url_get(h, CURLUPART_QUERY, &query, 0);
64 rc = curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
65 rc = curl_url_get(h, CURLUPART_USER, &user, 0);
66 rc = curl_url_get(h, CURLUPART_ZONEID, &zoneid, 0);
67
68 Extracted parts are not URL decoded unless the user also asks for it
69 with the CURLU_URLDECODE flag set in the fourth bitmask argument.
70
71 Remember to free the returned string with curl_free(3) when you are
72 done with it!
73
75 A user set individual URL parts, either after having parsed a full URL
76 or instead of parsing such.
77
78 rc = curl_url_set(urlp, CURLUPART_FRAGMENT, "anchor", 0);
79 rc = curl_url_set(urlp, CURLUPART_HOST, "www.example.com", 0);
80 rc = curl_url_set(urlp, CURLUPART_PASSWORD, "doe", 0);
81 rc = curl_url_set(urlp, CURLUPART_PATH, "/index.html", 0);
82 rc = curl_url_set(urlp, CURLUPART_PORT, "443", 0);
83 rc = curl_url_set(urlp, CURLUPART_QUERY, "name=john", 0);
84 rc = curl_url_set(urlp, CURLUPART_SCHEME, "https", 0);
85 rc = curl_url_set(urlp, CURLUPART_USER, "john", 0);
86 rc = curl_url_set(urlp, CURLUPART_ZONEID, "eth0", 0);
87
88 Set parts are not URL encoded unless the user asks for it with the
89 CURLU_URLENCODE flag.
90
92 An application can append a string to the right end of the query part
93 with the CURLU_APPENDQUERY flag to curl_url_set(3).
94
95 Imagine a handle that holds the URL "https://example.com/?shoes=2". An
96 application can then add the string "hat=1" to the query part like
97 this:
98
99 rc = curl_url_set(urlp, CURLUPART_QUERY, "hat=1", CURLU_APPENDQUERY);
100
101 It will even notice the lack of an ampersand (&) separator so it will
102 inject one too, and the handle's full URL will then equal "https://ex‐
103 ample.com/?shoes=2&hat=1".
104
105 The appended string can of course also get URL encoded on add, and if
106 asked to URL encode, the encoding process will skip the '=' character.
107 For example, append "candy=N&N" to what we already have, and URL encode
108 it to deal with the ampersand in the data:
109 rc = curl_url_set(urlp, CURLUPART_QUERY, "candy=N&N",
110 CURLU_APPENDQUERY | CURLU_URLENCODE);
111
112 Now the URL looks like
113 https://example.com/?shoes=2&hat=1&candy=N%26N
114
116 The URL API was introduced in libcurl 7.62.0.
117
118 A URL with a literal IPv6 address can be parsed even when IPv6 support
119 is not enabled.
120
122 curl_url(3), curl_url_cleanup(3), curl_url_get(3), curl_url_dup(3),
123 curl_url_set(3), curl_url_strerror(3), CURLOPT_URL(3)
124
125
126
127libcurl 8.2.1 June 14, 2023 libcurl(3)