0

I'm currently dealing with HTTP POST requests in C++ and I encountered an error. When I set my POST data with :

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata.c_str());

postdata.c_str() was equals :

cpp=1&data=zJBnu/hyK+xgm1tUOM22bL9h/9BVdqss7uc5uLHJ65lLHSTzQCSdgA36cz0/xb3juGGtSjm8P9m+4Ee6B6ot4IxMhuJBH5x3Co6L35tZRg7BhxTWO29UMtgitEo/OPBSiRVX/doZwlNBPm3v8ZRgjH8PT/W/VajqZYGvIvwK1cgBdrTeRCLE1nkEdQ2O0lg00VCU2amSBc4faty/RtTZYflpc+PQl9YKvsC+F09uZVKHORKfJPnIxg1SLUSuXNfzItJOR+VF1GVRcQmYDvVoW5JHiejMjnrVNnIMpmU35y0mzywZLYn1XUL9NhFHLTAGfxsttiTmZOLY7dgCBWTPng==&mode=0. It's some parameters as Key/Value pair. Data here is base64 encoded.

But when I did the curl_perform(), the value of data was not the same. All the + was replaced by spaces. So PHP base64_decode($_POST['data'], true); function return false.

I tried by different ways to avoid it C++ side but nothing works.

hassan
  • 7,812
  • 2
  • 25
  • 36
RedLens834
  • 231
  • 3
  • 18
  • 3
    Possible duplicate of [$\_POST Value Geting Plus Signs Removed](http://stackoverflow.com/questions/7410515/post-value-geting-plus-signs-removed) – ccKep May 03 '17 at 10:34
  • The solution don't talk about curl in C++. I can make a simple replacement but the problem can appear with others chars isn't it ? – RedLens834 May 03 '17 at 11:16
  • 1
    Well, Googling how to encode URLs shouldn't be that hard? The first result I got was [this SO question](http://stackoverflow.com/questions/154536/encode-decode-urls-in-c) mentioning [curl_easy_escape](https://curl.haxx.se/libcurl/c/curl_easy_escape.html). Your question made it sound like you had no idea **why** your pluses are getting replaced by spaces, so I took that as the main focus. – ccKep May 03 '17 at 11:21
  • I alwready try it ! Like I said in the post I already search for alternatives or fixs. But nothing works. I use previously curl_easy_escape. But server side when I did a print_r() of $_POST I got : `Array ( [?] => )` – RedLens834 May 03 '17 at 11:34
  • 1
    Then show your try on curl_easy_escape and post the URL that you got out of it. Without some code all we can really do is tell you that you're not escaping your URL properly and that's it. – ccKep May 03 '17 at 11:54
  • Okay, when I tried to get my url to paste it here I saw my error. I did a curl_easy_escape () on postdata. But I forgot that postdata contains more than one parameter so the curl function encode also the `=` and that could not readable by the server. My bad. Thanks! – RedLens834 May 03 '17 at 12:06
  • How can I mark it to resolved ? – RedLens834 May 03 '17 at 12:08
  • I've posted an answer, you can accept it (or post your own) to mark this question resolved. – ccKep May 03 '17 at 12:14

1 Answers1

4

You need to properly escape your URL, plus signs are interpreted as spaces (see link).

A few Links that might be helpful:

Be sure to escape each parameter individually, otherwise your ?, & and = chars (which are valid in those positions) might get replaced aswell.

Also note that curl_easy_escape allocates a new string which needs to be free'd by curl_free in order to avoid memory leaks.

Community
  • 1
  • 1
ccKep
  • 5,786
  • 19
  • 31