发送字符串与libcurl的PUT请求 [英] Send string in PUT request with libcurl
本文介绍了发送字符串与libcurl的PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的code是这样的:
卷曲= curl_easy_init();如果(卷曲){
标题= curl_slist_append(头,client_id_header);
标题= curl_slist_append(头,内容类型:应用程序/ JSON); curl_easy_setopt(卷曲,CURLOPT_HTTPHEADER头);
curl_easy_setopt(卷曲,CURLOPT_URL,127.0.0.1/test.php);
curl_easy_setopt(卷曲,CURLOPT_PUT,1L); RES = curl_easy_perform(卷曲);
RES = curl_easy_send(卷曲,json_struct,strlen的(json_struct),放大器; io_len); curl_slist_free_all(头);
curl_easy_cleanup(卷曲);
}
这不工作,该方案只是挂起,直到永远。
在test.php的这些请求头,我得到:
阵列(6){
[主机] =>
字符串(9)127.0.0.1
[接受] =>
串(3)* / *
[传输编码] =>
字符串(7)分块
[X-客户端Id] =>
串(36)PHP _...
[的Content-Type] =>
串(16)应用程序/ JSON
[期待] =>
串(12)100-继续
}
但身体是空的,意味着没有JSON数据与请求一起发送。
我想用的libcurl做的其实是什么都没有,然后这些命令行脚本:
卷曲-X PUT -H内容类型:应用程序/ JSON-d......一些JSON ......127.0.0.1/test.php
解决方案
明白了:)
不使用
curl_easy_setopt(卷曲,CURLOPT_PUT,1L);
请自定义请求和发送的数据为POSTFIELDS:
卷曲= curl_easy_init();如果(卷曲){
标题= curl_slist_append(头,client_id_header);
标题= curl_slist_append(头,内容类型:应用程序/ JSON); curl_easy_setopt(卷曲,CURLOPT_HTTPHEADER头);
curl_easy_setopt(卷曲,CURLOPT_URL,request_url);
curl_easy_setopt(卷曲,CURLOPT_CUSTOMREQUEST,PUT); / *! * / curl_easy_setopt(卷曲,CURLOPT_POSTFIELDS,json_struct); / *数据放在这里* / RES = curl_easy_perform(卷曲); curl_slist_free_all(头);
curl_easy_cleanup(卷曲);
}
My code looks like this:
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);
}
Which doesnt work, the program just hangs forever.
In test.php these are the request headers I get:
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"
}
But the body is empty, means, no json data is sent with the request.
What I want to do with libcurl is actually nothing else then these command line script:
curl -X PUT -H "Content-Type: application/json" -d '... some json ...' 127.0.0.1/test.php
解决方案
Got it :)
Dont use
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
Make a custom request and send the data as POSTFIELDS:
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, request_url);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_struct); /* data goes here */
res = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
这篇关于发送字符串与libcurl的PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文