Add paramethers to libcurl GET in c++

后端 未结 2 823
忘了有多久
忘了有多久 2021-01-24 10:32

I want to add some additional data in variable to HTTP GET using c++. When I make request using HTTP POST I do it like that:

    curl_easy_setopt(curl, CURLOPT_U         


        
相关标签:
2条回答
  • 2021-01-24 11:21

    Some support of this now exist (since curl version 7.62). The '?' and '&' are added automatically but I currently see no way of adding the parameters and values separately so they still have to be created with a '=' between them.

    // Create URL
    CURLUcode result;
    CURLU *url = curl_url();
    char *urlStr;
    
    result = curl_url_set(url, CURLUPART_URL, "https://example.com/hello.json", 0);
    
    if(!result) {
        const char paramValue1[] = "param1=value1";
        const char paramValue2[] = "param2=value2";
    
        // Add paramters
        result = curl_url_set(url, CURLUPART_QUERY, paramValue1, CURLU_APPENDQUERY);
        result = curl_url_set(url, CURLUPART_QUERY, paramValue2, CURLU_APPENDQUERY);
    
        // Convert URL to string for printing
        result = curl_url_get(url, CURLUPART_URL, &urlStr, 0);
    
        printf("New URL: %s", urlStr);
    }
    curl_url_cleanup(url);
    
    // Output: New URL: https://example.com/hello.json?param1=value1&param2=value2
    

    References

    [1] https://curl.haxx.se/libcurl/c/curl_url_set.html

    0 讨论(0)
  • 2021-01-24 11:25

    For GET just append the parameters to the URL, like

    http://some.host.com/some/path?variable1=value1&variable2=value2
    

    I'm sure you've seen it before!

    0 讨论(0)
提交回复
热议问题