Il mio codice è simile al seguente:Invia stringa nella richiesta PUT con libcurl
curl = curl_easy_init();
if (curl) {
headers = curl_slist_append(headers, client_id_header);
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1/test.php");
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
res = curl_easy_perform(curl);
res = curl_easy_send(curl, json_struct, strlen(json_struct), &io_len);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
Che pretende molto lavoro, il programma si blocca solo per sempre.
In test.php queste sono le intestazioni di richiesta ottengo:
array(6) {
["Host"]=>
string(9) "127.0.0.1"
["Accept"]=>
string(3) "*/*"
["Transfer-Encoding"]=>
string(7) "chunked"
["X-ClientId"]=>
string(36) "php_..."
["Content-Type"]=>
string(16) "application/json"
["Expect"]=>
string(12) "100-continue"
}
Ma il corpo è vuoto, significa, non ci sono dati JSON viene inviato con la richiesta.
Quello che voglio fare con libcurl è in realtà niente altro allora questi script della riga di comando:
curl -X PUT -H "Content-Type: application/json" -d '... some json ...' 127.0.0.1/test.php
Perfetto! E se si desidera impedire/sopprimere la risposta dal server, è necessario impostare 'CURLOPT_WRITEFUNCTION' per ricevere i dati o impostare' CURLOPT_WRITEDATA' su un diverso handle 'FILE * '. – karlphillip
Perché non usare 'CURLOPT_UPLOAD' invece di' CURLOPT_CUSTOMREQUEST', come la [documentazione] (http://curl.haxx.se/libcurl/c/CURLOPT_PUT.html) dice? –
Ho dovuto eseguire 'curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, NULL);' dopo "curl_easy_perform (curl);" per evitare che anche le richieste successive vengano richieste PUT. –