简体   繁体   中英

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

I've succeeded in my GET request with libcurl, no problems!

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..

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.

How can I send that data to the server is my question?

JSON data to be sent:

{
"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:

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

// use "encoded"

// free the memory
curl_free(encoded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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