溫馨提示×

c++ post如何設置Content-Type

c++
小樊
154
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女