c++

c++ post如何设置Content-Type

小樊
104
2024-07-05 20:09:27
栏目: 编程语言

在C++中,你可以使用HTTP请求头来设置Content-Type。以下是一个示例代码:

#include <iostream>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        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;
}

在上面的代码中,我们使用了libcurl库来发送HTTP请求,并在请求头中设置了Content-Type为application/json。你可以根据需要修改Content-Type的值。

0
看了该问题的人还看了