1*6236dae4SAndroid Build Coastguard Worker--- 2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 4*6236dae4SAndroid Build Coastguard WorkerTitle: curl_easy_escape 5*6236dae4SAndroid Build Coastguard WorkerSection: 3 6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl 7*6236dae4SAndroid Build Coastguard WorkerSee-also: 8*6236dae4SAndroid Build Coastguard Worker - curl_easy_unescape (3) 9*6236dae4SAndroid Build Coastguard Worker - curl_url_set (3) 10*6236dae4SAndroid Build Coastguard Worker - curl_url_get (3) 11*6236dae4SAndroid Build Coastguard WorkerProtocol: 12*6236dae4SAndroid Build Coastguard Worker - All 13*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.15.4 14*6236dae4SAndroid Build Coastguard Worker--- 15*6236dae4SAndroid Build Coastguard Worker 16*6236dae4SAndroid Build Coastguard Worker# NAME 17*6236dae4SAndroid Build Coastguard Worker 18*6236dae4SAndroid Build Coastguard Workercurl_easy_escape - URL encode a string 19*6236dae4SAndroid Build Coastguard Worker 20*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS 21*6236dae4SAndroid Build Coastguard Worker 22*6236dae4SAndroid Build Coastguard Worker~~~c 23*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h> 24*6236dae4SAndroid Build Coastguard Worker 25*6236dae4SAndroid Build Coastguard Workerchar *curl_easy_escape(CURL *curl, const char *string, int length); 26*6236dae4SAndroid Build Coastguard Worker~~~ 27*6236dae4SAndroid Build Coastguard Worker 28*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION 29*6236dae4SAndroid Build Coastguard Worker 30*6236dae4SAndroid Build Coastguard WorkerThis function converts the given input *string* to a URL encoded string and 31*6236dae4SAndroid Build Coastguard Workerreturns that as a new allocated string. All input characters that are not a-z, 32*6236dae4SAndroid Build Coastguard WorkerA-Z, 0-9, '-', '.', '_' or '~' are converted to their "URL escaped" version 33*6236dae4SAndroid Build Coastguard Worker(**%NN** where **NN** is a two-digit hexadecimal number). 34*6236dae4SAndroid Build Coastguard Worker 35*6236dae4SAndroid Build Coastguard WorkerIf *length* is set to 0 (zero), curl_easy_escape(3) uses strlen() on the input 36*6236dae4SAndroid Build Coastguard Worker*string* to find out the size. This function does not accept input strings 37*6236dae4SAndroid Build Coastguard Workerlonger than **CURL_MAX_INPUT_LENGTH** (8 MB). 38*6236dae4SAndroid Build Coastguard Worker 39*6236dae4SAndroid Build Coastguard WorkerYou must curl_free(3) the returned string when you are done with it. 40*6236dae4SAndroid Build Coastguard Worker 41*6236dae4SAndroid Build Coastguard Worker# ENCODING 42*6236dae4SAndroid Build Coastguard Worker 43*6236dae4SAndroid Build Coastguard Workerlibcurl is typically not aware of, nor does it care about, character 44*6236dae4SAndroid Build Coastguard Workerencodings. curl_easy_escape(3) encodes the data byte-by-byte into the 45*6236dae4SAndroid Build Coastguard WorkerURL encoded version without knowledge or care for what particular character 46*6236dae4SAndroid Build Coastguard Workerencoding the application or the receiving server may assume that the data 47*6236dae4SAndroid Build Coastguard Workeruses. 48*6236dae4SAndroid Build Coastguard Worker 49*6236dae4SAndroid Build Coastguard WorkerThe caller of curl_easy_escape(3) must make sure that the data passed in 50*6236dae4SAndroid Build Coastguard Workerto the function is encoded correctly. 51*6236dae4SAndroid Build Coastguard Worker 52*6236dae4SAndroid Build Coastguard Worker# URLs 53*6236dae4SAndroid Build Coastguard Worker 54*6236dae4SAndroid Build Coastguard WorkerURLs are by definition *URL encoded*. To create a proper URL from a set of 55*6236dae4SAndroid Build Coastguard Workercomponents that may not be URL encoded already, you cannot just URL encode the 56*6236dae4SAndroid Build Coastguard Workerentire URL string with curl_easy_escape(3), because it then also converts 57*6236dae4SAndroid Build Coastguard Workercolons, slashes and other symbols that you probably want untouched. 58*6236dae4SAndroid Build Coastguard Worker 59*6236dae4SAndroid Build Coastguard WorkerTo create a proper URL from strings that are not already URL encoded, we 60*6236dae4SAndroid Build Coastguard Workerrecommend using libcurl's URL API: set the pieces with curl_url_set(3) and get 61*6236dae4SAndroid Build Coastguard Workerthe final correct URL with curl_url_get(3). 62*6236dae4SAndroid Build Coastguard Worker 63*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS% 64*6236dae4SAndroid Build Coastguard Worker 65*6236dae4SAndroid Build Coastguard Worker# EXAMPLE 66*6236dae4SAndroid Build Coastguard Worker 67*6236dae4SAndroid Build Coastguard Worker~~~c 68*6236dae4SAndroid Build Coastguard Workerint main(void) 69*6236dae4SAndroid Build Coastguard Worker{ 70*6236dae4SAndroid Build Coastguard Worker CURL *curl = curl_easy_init(); 71*6236dae4SAndroid Build Coastguard Worker if(curl) { 72*6236dae4SAndroid Build Coastguard Worker char *output = curl_easy_escape(curl, "data to convert", 15); 73*6236dae4SAndroid Build Coastguard Worker if(output) { 74*6236dae4SAndroid Build Coastguard Worker printf("Encoded: %s\n", output); 75*6236dae4SAndroid Build Coastguard Worker curl_free(output); 76*6236dae4SAndroid Build Coastguard Worker } 77*6236dae4SAndroid Build Coastguard Worker curl_easy_cleanup(curl); 78*6236dae4SAndroid Build Coastguard Worker } 79*6236dae4SAndroid Build Coastguard Worker} 80*6236dae4SAndroid Build Coastguard Worker~~~ 81*6236dae4SAndroid Build Coastguard Worker 82*6236dae4SAndroid Build Coastguard Worker# HISTORY 83*6236dae4SAndroid Build Coastguard Worker 84*6236dae4SAndroid Build Coastguard WorkerSince 7.82.0, the **curl** parameter is ignored. Prior to that there was 85*6236dae4SAndroid Build Coastguard Workerper-handle character conversion support for some old operating systems such as 86*6236dae4SAndroid Build Coastguard WorkerTPF, but it was otherwise ignored. 87*6236dae4SAndroid Build Coastguard Worker 88*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY% 89*6236dae4SAndroid Build Coastguard Worker 90*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE 91*6236dae4SAndroid Build Coastguard Worker 92*6236dae4SAndroid Build Coastguard WorkerA pointer to a null-terminated string or NULL if it failed. 93