Adapted from https://stackoverflow.com/a/29962178/3848666
| 33 | |
| 34 | // Adapted from https://stackoverflow.com/a/29962178/3848666 |
| 35 | std::string urlEncode(const std::string& input) { |
| 36 | std::string result = ""; |
| 37 | const char* characters = input.c_str(); |
| 38 | char hex_buffer[10]; |
| 39 | size_t input_length = input.length(); |
| 40 | |
| 41 | for (size_t i = 0;i < input_length;i++) { |
| 42 | unsigned char c = characters[i]; |
| 43 | // uncomment this if you want to encode spaces with + |
| 44 | if (c==' ') { |
| 45 | result += '+'; |
| 46 | } else if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { |
| 47 | result += c; |
| 48 | } else { |
| 49 | sprintf(hex_buffer, "%%%02X", c); //%% means '%' literal, %02X means at least two digits, paddable with a leading zero |
| 50 | result += hex_buffer; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | // Adapted from https://stackoverflow.com/a/29962178/3848666 |
| 58 | std::string urlDecode(const std::string& input) { |