简体   繁体   English

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

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

I've succeeded in my GET request with libcurl, no problems!我用 libcurl 成功完成了我的 GET 请求,没问题!

However when I try to send a post request, I'm unsure of where I put the json data, I want to be sent over..但是,当我尝试发送发布请求时,我不确定将 json 数据放在哪里,我想被发送过去..

My code looks like this, and I wonder if there is a method where I can send over the data as is:我的代码看起来像这样,我想知道是否有一种方法可以按原样发送数据:

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
}")

That doesn't work and is the raw JSON data I want to send to the server.这不起作用,是我想发送到服务器的原始 JSON 数据。

How can I send that data to the server is my question?我如何将数据发送到服务器是我的问题?

JSON data to be sent: JSON 待发送数据:

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

   

Use curl_easy_escape to URL encode the given C string:使用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