簡體   English   中英

如何使用 curl 將 POST 請求從 python 重寫為 C++

[英]How to rewrite POST request from python to C++ with curl

我在 python 上有很多設置的 POST 請求,我不明白它們在 curl 中的樣子。

data_str = '{' + '"username": "{}", "domain_id": {}, "password": {}'.format(login, domain_id, password) + '}'
      try:
            data = requests.post("https://example.com/v/session",
                                 proxies=proxy,
                                 verify=False,
                                 data=data_str,
                                 headers={"Content-Type": "application/json;charset=UTF-8",
                                          "Accept": "application/json"})
            if is_json(data.text):
                print(data)

我發現 url 設置參數 CURLOPT_URL,標題 - CURLOPT_HTTPHEADER。 但是如何設置代理,驗證,數據? 如何在 python 中獲取 json ? 如何完成與python中結果相同的代碼:

CURL *curl = curl_easy_init();

struct curl_slist *list = NULL;

if(curl) {
 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

 list = curl_slist_append(list, "Shoesize: 10");
 list = curl_slist_append(list, "Accept:");

 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

 curl_easy_perform(curl);

 curl_slist_free_all(list); /* free the list again */
}

為了從 curl 請求中獲取返回數據,我們需要一個 CURLOPT_WRITEFUNCTION 選項的回調函數。 proxydataverify參數設置如下:

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

size_t curlWriter(void *contents, size_t size, size_t nmemb, std::string *s)
{
    size_t newLength = size*nmemb;
    try
    {
        s->append((char*)contents, newLength);
    }
    catch(std::bad_alloc &e)
    {
        //memory problem
        return 0;
    }
    return newLength;
}
int main()
{
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if(curl)
    {
        std::string strResponse;
        std::string strPostData = "my data post";

        curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/v/session");
        curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); 

        //set the proxy
        curl_easy_setopt(curl, CURLOPT_PROXY, "http://proxy.net");  
        curl_easy_setopt(curl, CURLOPT_PROXYPORT, 8080L);

        //verify=False. SSL checking disabled
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); 


        //set the callback function
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriter);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strResponse);


        /* size of the POST data */
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strPostData.length() );

        /* pass in a pointer to the data - libcurl will not copy */
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPostData.c_str() );

        /* Execute the request */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if(res != CURLE_OK)
        {
            std::cerr <<  "CURL error : " << curl_easy_strerror(res) << std::endl;
        }else {
            std::cout <<  "CURL result : " << strResponse << std::endl;
        }

        curl_easy_cleanup(curl);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM