1

Yeah weird title, I don't know what it should be.

I have a dll, and after a long time trying and finally figuring out how I could upload things to a webhook, it finally works. But, when I try to upload a string which contains a +, the + will be replaced with a " ".
Example:

Uploaded: FRvERUb9xgMlP4BS+bm+t+CLI6cG026vbtev2gSbBYM= 
Received: FRvERUb9xgMlP4BS bm t CLI6cG026vbtev2gSbBYM=

I want to know how I can upload it so that the thing will also send the characters +.
The code for uploading:

void sendmsg() {
    CURL* curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "my webhook");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "content=FRvERUb9xgMlP4BS+bm+t+CLI6cG026vbtev2gSbBYM=");
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
}

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Tyrone
  • 11
  • 1
  • Have you tried replacing all the `+` characters with `%2B`? – Stack Underflow Dec 15 '21 at 19:32
  • A duplicate, but also one with no answers. A deleted answer suggests [`curl_easy_escape()`](https://curl.se/libcurl/c/curl_easy_escape.html) may be the answer. https://stackoverflow.com/questions/43552307/curl-api-is-replacing-with-spaces – Drew Dormann Dec 15 '21 at 19:32
  • 1
    @StackUnderflow well actually, the string is not even placed there by me but the dll takes it from some data. So I could try to replace + with that but i dont know, it might just upload it with the %2B instead of +. Idk I'll try – Tyrone Dec 15 '21 at 19:35

1 Answers1

3

This POST is a normal application/x-www-form-urlencoded kind (and libcurl will set that Content-Type by default when this option is used), which is commonly used by HTML forms.

You can use curl_easy_escape to url-encode your data, if necessary. It returns a pointer to an encoded string that can be passed as postdata.

if (curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "my webhook");
  char* content = curl_easy_escape(curl, "FRvERUb9xgMlP4BS+bm+t+CLI6cG026vbtev2gSbBYM=", 0);
  if (content) {
     curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, ("content="s + content).c_str());
     curl_free(content);
     res = curl_easy_perform(curl);
  }
  curl_easy_cleanup(curl);
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    You are creating a *temporary* `std::string` and giving its data pointer to curl. When that `curl_easy_setopt()` call exits, the temporary will be destroyed, leaving curl with an invalid pointer. `curl_easy_perform()` requires a `CURLOPT_POSTFIELDS` pointer to remain valid during the transfer, this is explicitly stated in the [documentation](https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html). If you are going to use a temporary string like this, use [`CURLOPT_COPYPOSTFIELDS`](https://curl.se/libcurl/c/CURLOPT_COPYPOSTFIELDS.html) instead. Otherwise, save the string in a local variable first. – Remy Lebeau Dec 16 '21 at 05:18