发送带有 libcurl C++ 问题的 post 请求:s

Send a post request with libcurl C++ question :s

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

然而,当我尝试发送 post 请求时,我不确定我将 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对给定的C字符串进行URL编码:

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

// use "encoded"

// free the memory
curl_free(encoded);