problem with lubcurl curl_formadd data type

Actually since I am still sort of new to c++, I don't think my problem is really with "curl_formadd" but something to do with the data type that I am passing to it. I don't know... that's why I'm here.

Generally "curl_formadd" is working for some of the info that I'm sending to my server, just not all of it. Basically, I am trying to upload an image, and I need to send some data with that image so I know what to do with it when it gets there. Pretty straightforward. My abridged code looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//info from file when object initialized
if (file.is_open()) {
    while (file >> myExample::username >> myExample::password >> myExample::someDate) {}
}
file.close();

//post image & member data, etc. to server
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "imageUpload",
    CURLFORM_FILE, myExample::testImage, CURLFORM_END);
std::string foo = "bar";
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "fooBar",
    CURLFORM_COPYCONTENTS, &foo, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "user",
    CURLFORM_COPYCONTENTS, myExample::username, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "pass",
    CURLFORM_COPYCONTENTS, myExample::password, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "time",
    CURLFORM_COPYCONTENTS, std::to_string(myExample::someDate), CURLFORM_END);


The above code is pretty much right out of the libcurl documentation, and in fact some of the fields in this example successfully find their way to the server. The image always uploads, and both "foo" and "myExample::someDate" get there as well. On the other hand, when "myExample::username" and "myExample::password" get to the server they both arrive as random gibberish. When I write everything to a text file on the server side it looks like this:

image: test-image.png
fooBar: bar
user: ð€j|
pass: à†j|
registration: 1459335380

I don't know if the foreign characters will display here, but the point is that something is coming to the server but not in the expected form. I have tried suppling a pointer to the problem fields as I have done with "foo", but I am obviously doing some wrong because the program crashes when I try to run it. I don't think this is the issue though, because if I change the argument from "&foo" to "foo" I still receive "bar" at the other end.

My only guess is that the data being read from the file is somehow not the correct data type or something of the sort. My reasoning is this: when I changed "foo" to member data, so "std::string myExample::foo" instead, and pass this (not a pointer) to "curl_formadd" - I successfully receive "bar" as expected. Both "user" and "pass" are "std::string" the same as "foo", but this information, the only string data from file, is the only information not getting to the server.
Take a look at this:

https://curl.haxx.se/libcurl/c/curl_formadd.html

It is not that clear but "If the data isn't null terminated ..." hints to a c-string not a pointer to std::string. So I would think it should look likes:
1
2
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "fooBar",
    CURLFORM_COPYCONTENTS, foo.c_str(), CURLFORM_END);
Well spotted, coder777!! That worked perfectly! My final code looked a bit like this:

1
2
3
4
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "user",
    CURLFORM_COPYCONTENTS, myExample::username.c_str(), CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "pass",
    CURLFORM_COPYCONTENTS, myExample::password.c_str(), CURLFORM_END)


My server is happy now. Thanks.
Topic archived. No new replies allowed.