Note that there are some explanatory texts on larger screens.

plurals
  1. POCurl will not post multiple data to server
    primarykey
    data
    text
    <p>I am writing an application for work that needs to perform HTTP requests to a server, and get a response, in JSON back.</p> <p>At the moment, my code connects to the server, and gets a response back, which is great. However, I also need to send data to the server, which will process it, and send me the jSON.</p> <p>My problem is that I am able to send, thanks to POSTFIELDS, only a single "field", and won't get any response if I insert more that one.</p> <p>The code is the following:</p> <pre><code>// writefunc works, so there is no point adding its code in here size_t Simulator::writefunc(void *ptr, size_t size, size_t nmemb, struct string *buffer_in); void Simulator::move(Player *player) { std::ostringstream ss_url; ss_url &lt;&lt; API_URL &lt;&lt; "functionCalled"; char const *url = ss_url.str().c_str(); std::ostringstream ss_postfields; // This will not work - And all values do NOT contain any space // The server do NOT receive any data (in POST and GET) ss_postfields &lt;&lt; "user_name=" &lt;&lt; player-&gt;getName() &lt;&lt; "&amp;user_secret=" &lt;&lt; player-&gt;secret() &lt;&lt; "&amp;app_id=" &lt;&lt; player-&gt;getApp_id() &lt;&lt; "&amp;lat=" &lt;&lt; player-&gt;getLocation()-&gt;getLat() &lt;&lt;"&amp;lon=" &lt;&lt; player-&gt;getLocation()-&gt;getLon(); // This will not work either // ss_postfields &lt;&lt; "user_name=nicolas&amp;app_id=2"; // This works and will send the data to the server, which will receive and process it. // ss_postfields &lt;&lt; "user_name=" &lt;&lt; player-&gt;getName(); const char *postfields = ss_postfields.str().c_str(); CURL *curl_handle; curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); if(curl_handle){ struct string s; init_string(&amp;s); curl_easy_setopt(curl_handle, CURLOPT_URL, url); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writefunc); curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &amp;s); curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postfields); curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(postfields)); curl_easy_setopt(curl_handle, CURLOPT_POST, 1L); CURLcode res = curl_easy_perform(curl_handle); if(CURLE_OK != res) { printf("Error: %s\n", strerror(res)); exit(0); } printf("%s\n", s.ptr); curl_easy_cleanup(curl_handle); curl_global_cleanup(); } } </code></pre> <p>I thought I would also give the output of CURLOPT_HEADER and CURLOPT_VERBOSE for when I send only 1 value, or multiple values:</p> <p>When I Send One value only:</p> <pre><code>* About to connect() to localhost port 8888 (#0) * Trying ::1... * connected * Connected to localhost (::1) port 8888 (#0) &gt; POST [api_url] HTTP/1.1 Host: localhost:8888 Accept: */* Content-Length: 22 Content-Type: application/x-www-form-urlencoded * upload completely sent off: 22 out of 22 bytes &lt; HTTP/1.1 200 OK &lt; Date: Sun, 24 Nov 2013 17:25:14 GMT &lt; Server: Apache &lt; X-Powered-By: PHP/5.3.20 &lt; Cache-Control: no-cache &lt; X-Debug-Token: a41727 &lt; Transfer-Encoding: chunked &lt; Content-Type: application/json &lt; * Connection #0 to host localhost left intact 0 HTTP/1.1 200 OK Date: Sun, 24 Nov 2013 17:25:14 GMT Server: Apache X-Powered-By: PHP/5.3.20 Cache-Control: no-cache X-Debug-Token: a41727 Transfer-Encoding: chunked Content-Type: application/json [ OUTPUT FROM SERVER HERE ] * Closing connection #0 </code></pre> <p>And when I send multiple values:</p> <pre><code>* About to connect() to localhost port 8888 (#0) * Trying ::1... * connected * Connected to localhost (::1) port 8888 (#0) &gt; POST [api_url] HTTP/1.1 Host: localhost:8888 Accept: */* Content-Length: 1 Content-Type: application/x-www-form-urlencoded * upload completely sent off: 1 out of 1 bytes &lt; HTTP/1.1 200 OK &lt; Date: Sun, 24 Nov 2013 17:30:13 GMT &lt; Server: Apache &lt; X-Powered-By: PHP/5.3.20 &lt; Cache-Control: no-cache &lt; X-Debug-Token: d1947e &lt; Transfer-Encoding: chunked &lt; Content-Type: application/json &lt; * Connection #0 to host localhost left intact 0 HTTP/1.1 200 OK Date: Sun, 24 Nov 2013 17:30:13 GMT Server: Apache X-Powered-By: PHP/5.3.20 Cache-Control: no-cache X-Debug-Token: d1947e Transfer-Encoding: chunked Content-Type: application/json [ OUTPUT FROM SERVER HERE ] * Closing connection #0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload