繁体   English   中英

使用 libcurl C++ 发送帖子请求 问题:s

[英]Send a post request with libcurl C++ question :s

我用 libcurl 成功完成了我的 GET 请求,没问题!

但是,当我尝试发送发布请求时,我不确定将 json 数据放在哪里,我想被发送过去..

我的代码看起来像这样,我想知道是否有一种方法可以按原样发送数据:

CURL* curlpost;

curl_global_init(CURL_GLOBAL_ALL);

curlpost = curl_easy_init();
if (curlpost) {
    curl_easy_setopt(curlpost, CURLOPT_URL, "https://127.0.0.1:50006/lol-lobby/v2/lobby");
    // post data:
    curl_easy_setopt(curlpost, CURLOPT_POSTFIELDS, "{

        "customGameLobby": {
        "configuration": {
            "gameMode": "CLASSIC", "gameMutator" : "", "gameServerRegion" : "",
                "mapId" : 11,
                "mutators" : {"id": 1}, "spectatorPolicy" : "AllAllowed", "teamSize" : 5
        }

    },
        "queueId": 830,
        "isCustom" : false
}")

这不起作用,是我想发送到服务器的原始 JSON 数据。

我如何将数据发送到服务器是我的问题?

JSON 待发送数据:

{
"customGameLobby": {
    "configuration": {
      "gameMode": "CLASSIC",
      "gameMutator": "",
      "gameServerRegion": "",
      "mapId": 11, 
      "mutators": {"id": 1},
      "spectatorPolicy": "AllAllowed",
      "teamSize": 5 
    }
  },
"queueId": 830,
"isCustom": false
}

   

使用curl_easy_escape对 URL 编码给定的 C 字符串:

char* encoded = curl_easy_escape(curlpost, "{ your post data }", 0);

// use "encoded"

// free the memory
curl_free(encoded);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM