libcurl 不发送 json-数据
libcurl does not send json-Data
我尝试将一些 json-代码从 c++ 传递到 Python Flask Rest Api。但不幸的是,这不起作用并且没有看到我的错误:(
这是我的 C 代码:
#include <stdio.h>
#include <curl/curl.h>
#include <string>
using namespace std;
int main(void)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/todo/api/v1.0/tasks/debug");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"title\" : \"The Title\"}");
struct curl_slist *headers = NULL;
curl_slist_append(headers, "Accept: application/json");
curl_slist_append(headers, "Content-Type: application/json");
curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
这是烧瓶函数:
#POST DEBUG
@app.route("/todo/api/v1.0/tasks/debug", methods=['POST'])
def echo():
print(request.json)
return "ReturnString \n"
flask-Server 的输出如下所示:
None
127.0.0.1 - - [12/Jul/2017 12:49:15] "POST /todo/api/v1.0/tasks/debug HTTP/1.1" 200 -
所以在我看来 json-Data 没有传递给 flask-function。
我尝试了与文本基本相同的方法并且它起作用了。
当我从命令行使用 curl-Call 尝试同样的事情时,它起作用了。
卷曲命令:
curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5000/todo/api/v1.0/tasks/debug
Flask-Server 的输出:
{'title': 'Read a book'}
127.0.0.1 - - [12/Jul/2017 13:11:54] "POST /todo/api/v1.0/tasks/debug HTTP/1.1" 200 -
有帮助吗?
在我 运行 您的代码并使用 Wireshark 查看 HTTP 请求后,我发现 Content-Type
header 未设置为 application/json
。然后我将您的代码与 curl ... --libcurl example.c
生成的代码进行了比较,发现必须像这样添加请求 header:
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
我尝试将一些 json-代码从 c++ 传递到 Python Flask Rest Api。但不幸的是,这不起作用并且没有看到我的错误:(
这是我的 C 代码:
#include <stdio.h>
#include <curl/curl.h>
#include <string>
using namespace std;
int main(void)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "localhost:5000/todo/api/v1.0/tasks/debug");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"title\" : \"The Title\"}");
struct curl_slist *headers = NULL;
curl_slist_append(headers, "Accept: application/json");
curl_slist_append(headers, "Content-Type: application/json");
curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
这是烧瓶函数:
#POST DEBUG
@app.route("/todo/api/v1.0/tasks/debug", methods=['POST'])
def echo():
print(request.json)
return "ReturnString \n"
flask-Server 的输出如下所示:
None
127.0.0.1 - - [12/Jul/2017 12:49:15] "POST /todo/api/v1.0/tasks/debug HTTP/1.1" 200 -
所以在我看来 json-Data 没有传递给 flask-function。 我尝试了与文本基本相同的方法并且它起作用了。
当我从命令行使用 curl-Call 尝试同样的事情时,它起作用了。 卷曲命令:
curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5000/todo/api/v1.0/tasks/debug
Flask-Server 的输出:
{'title': 'Read a book'}
127.0.0.1 - - [12/Jul/2017 13:11:54] "POST /todo/api/v1.0/tasks/debug HTTP/1.1" 200 -
有帮助吗?
在我 运行 您的代码并使用 Wireshark 查看 HTTP 请求后,我发现 Content-Type
header 未设置为 application/json
。然后我将您的代码与 curl ... --libcurl example.c
生成的代码进行了比较,发现必须像这样添加请求 header:
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");