libcurl发送http请求,接收到的数据有问题

libcurl sends http request, there is a problem with the received data


typedef struct ResponseBody {
    size_t memorySize = BUFSIZ;
    size_t dataSize{};
    char *bodyMemory = new char[BUFSIZ];

public:
    ~ResponseBody();
} ResponseBody;

ResponseBody::~ResponseBody() {
    delete[] this->bodyMemory;
}

size_t get_containers_callback(const char *buff, size_t size, size_t buff_size, void *data) {
    auto *body = (ResponseBody *) data;

    size_t needMemory = body->dataSize + buff_size;
    if (needMemory > body->memorySize) {
        auto *newMemory = new char[needMemory];
        strcpy(newMemory, body->bodyMemory);
        delete[] body->bodyMemory;
        body->bodyMemory = newMemory;
        body->memorySize = needMemory;
    }

    memcpy(body->bodyMemory + body->dataSize, buff, buff_size);
    body->dataSize += buff_size;
    return size * buff_size;
}

int main() {
    auto *responseBody = new ResponseBody;
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/containers/json?all=true");

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_containers_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, responseBody);

    res = curl_easy_perform(curl);
    if (res == CURLE_OK) {
        fprintf(stdout, "%s\n", responseBody->bodyMemory);
    }
    return 0;
}

服务器是docker,这是请求成功的结果:

[{"Id":"9467b3bee76d8b7f4212ddff890e0c46334d4f181f51652871e1d1a047f43d3e","Names":["/barrage-crawler-server"],"Image":"barrage-crawler-server","ImageID":"sha256:ea42e0cd2ad4505fbdad4add7d40c9e9f79ab932804db12e43ec898ea1659032","Command":"java -Xmx4G -Xms4G -Xloggc:logs/gc.log -XX:+HeapDumpOnOutOfMemoryError -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=6000 -Dcom.sun.management.jmxremote.rmi.port=6000 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.119 -jar barrage-crawler-server.jar","Created":1634116299,"Ports":[{"IP":"0.0.0.0","PrivatePort":8080,"PublicPort":8080,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":6000,"PublicPort":6000,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":5005,"PublicPort":5010,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up 6 hours","HostConfig":{"NetworkMode":"static"},"NetworkSettings":{"Networks":{"static":{"IPAMConfig":{"IPv4Address":"192.168.98.2"},"Links":null,"Aliases":null,"NetworkID":"9f82c2510202a41a9ea489c6ba3626d2970808acf843fc36f3f4154910847e1d","EndpointID":"6d1cca5727b935ce78cc7c74b881c55e27b928df2d93ed4b4f16d6cbeb752834","Gateway":"192.168.98.1","IPAddress":"192.168.98.2","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:c0:a8:62:02"}}},"Mounts":[]},{"Id":"cfbc21516759c40372aba8e777bc0c12b0f7ff7b2be1c2494b28d03de8b8fc60","Names":["/automatic-test"],"Image":"automatic-test","ImageID":"sha256:83ddc4b1ab867ac917c098c434c09df9d11063e43d04e8a5bed2353675d84609","Command":"java -Xms512M -Xmx512M -jar automatic-test-jar-with-dependencies.jar","Created":1634113207,"Ports":[],"Labels":{},"State":"running","Status":"Up 7 hours","HostConfig":{"NetworkMode":"static"},"NetworkSettings":{"Networks":{"static":{"IPAMConfig":{"IPv4Address":"192.168.98.3"},"Links":null,"Aliases":null,"NetworkID":"9f82c2510202a41a9ea489c6ba3626d2970808acf843fc36f3f4154910847e1d","EndpointID":"e191fc43f4a598d96c721ec186dd778aee8efd8a404db7ef381ec29f03cc6029","Gateway":"192.168.98.1","IPAddress":"192.168.98.3","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:c0:a8:62:03"}}},"Mounts":[]},{"Id":"967074c17f846d7aad0fc60329d6d57674f6715bc77fc6091f7f566487c5597e","Names":["/gitea"],"Image":"gitea/gitea","ImageID":"sha256:fb3d5df4d2978f50e0d6168029746739fc0c8110da8d6f8e3771deb715c3e093","Command":"/usr/bin/entrypoint /bin/s6-svscan /etc/s6","Created":1634055325,"Ports":[{"IP":"0.0.0.0","PrivatePort":3000,"PublicPort":3000,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":22,"PublicPort":3001,"Type":"tcp"}],"Labels":{"maintainer":"maintainers@gitea.io","org.opencontainers.image.created":"2021-05-14T05:16:51Z","org.opencontainers.image.revision":"44286e29f0a96e0228ebca567b6694e7b2ffccd3","org.opencontainers.image.source":"https://github.com/go-gitea/gitea.git","org.opencontainers.image.url":"https://github.com/go-gitea/gitea"},"State":"running","Status":"Up 23 hours","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"a6627d41463c4be87184cf29598b90bce10231e2304e39af01fd128e6aee079f","EndpointID":"3145eacea9b6459bf22728afecc497d2bac2a59895b273946bb7762d10593910","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}},"Mounts":[{"Type":"bind","Source":"/home/gitea/config","Destination":"/data/gitea","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/gitea/git","Destination":"/data/git","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"volume","Name":"133e605b5bca962cd67f51fc0da7f3628fe7670d920f01213345e4bb7365962f","Source":"/var/lib/docker/volumes/133e605b5bca962cd67f51fc0da7f3628fe7670d920f01213345e4bb7365962f/_data","Destination":"/data","Driver":"local","Mode":"","RW":true,"Propagation":""}]},{"Id":"e2da15ab5ccfb97ad2080c84e964d435d0a32a8cecffdf581a8a31b3e9241822","Names":["/redis"],"Image":"redis","ImageID":"sha256:de974760ddb2f32dbddb74b7bb8cff4c1eee06d43d36d11bbca1dc815173916e","Command":"docker-entrypoint.sh redis-server","Created":1634032228,"Ports":[],"Labels":{},"State":"running","Status":"Up 29 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"41d494a74057dc669dd3ad8f411da552067e291f2410d321f014e30a537742e9","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/redis/data","Destination":"/data","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"f8c79216291d5905a2327f3a1cbbf02da9e5e6112c712c45f0e98b25f68c8111","Names":["/mongo"],"Image":"mongo","ImageID":"sha256:30b3be246e3953807bd3ec956b05f6018548de547f457fa00890e5d4684dc68a","Command":"docker-entrypoint.sh mongod","Created":1634032015,"Ports":[],"Labels":{},"State":"running","Status":"Up 29 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"4468d6246ad3397d0529ac068f30b2f324d8d3dd7e07f58efb18df8209620162","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/mongo/data","Destination":"/data/db","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"volume","Name":"377e5e25c83db03bebbadb8a556ee009cc64448dd9d80f39030b9dbd31bb337c","Source":"/var/lib/docker/volumes/377e5e25c83db03bebbadb8a556ee009cc64448dd9d80f39030b9dbd31bb337c/_data","Destination":"/data/configdb","Driver":"local","Mode":"","RW":true,"Propagation":""}]},{"Id":"5fca4c5aca0295d5661064229bb17643572637eafebc41d12348418720570d50","Names":["/elasticsearch"],"Image":"docker.elastic.co/elasticsearch/elasticsearch:7.9.2","ImageID":"sha256:caa7a21ca06ea2376dc2a2b3194795def7bed7cc1a4de18feee0a706518d2c39","Command":"/tini -- /usr/local/bin/docker-entrypoint.sh eswrapper","Created":1634031546,"Ports":[],"Labels":{"org.label-schema.build-date":"2020-09-23T00:45:33.626720Z","org.label-schema.license":"Elastic-License","org.label-schema.name":"Elasticsearch","org.label-schema.schema-version":"1.0","org.label-schema.url":"https://www.elastic.co/products/elasticsearch","org.label-schema.usage":"https://www.elastic.co/guide/en/elasticsearch/reference/index.html","org.label-schema.vcs-ref":"d34da0ea4a966c4e49417f2da2f244e3e97b4e6e","org.label-schema.vcs-url":"https://github.com/elastic/elasticsearch","org.label-schema.vendor":"Elastic","org.label-schema.version":"7.9.2","org.opencontainers.image.created":"2020-09-23T00:45:33.626720Z","org.opencontainers.image.documentation":"https://www.elastic.co/guide/en/elasticsearch/reference/index.html","org.opencontainers.image.licenses":"Elastic-License","org.opencontainers.image.revision":"d34da0ea4a966c4e49417f2da2f244e3e97b4e6e","org.opencontainers.image.source":"https://github.com/elastic/elasticsearch","org.opencontainers.image.title":"Elasticsearch","org.opencontainers.image.url":"https://www.elastic.co/products/elasticsearch","org.opencontainers.image.vendor":"Elastic","org.opencontainers.image.version":"7.9.2"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"04fa8af3719eae67d2d99c0b3216cef2c3392a7546f575f7d7ebd790d6f17378","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/elasticsearch/config","Destination":"/usr/share/elasticsearch/config","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/elasticsearch/data","Destination":"/var/lib/elasticsearch","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/elasticsearch/logs","Destination":"/var/log/elasticsearch","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"8c805aa531b49648440d06e0c124b37e054822c8c419fd25338277dd08881d58","Names":["/mysqld"],"Image":"mysql:5.7.25","ImageID":"sha256:98455b9624a96e32b353297bb312913b6bbd62ac195cea2c7dd477209ba572d6","Command":"docker-entrypoint.sh mysqld","Created":1634030551,"Ports":[],"Labels":{},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"f16d5a1abb34d1917197b5d0125541da147cdfeb90ec07f61a5e5b754a1c3a48","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/mysql/data","Destination":"/var/lib/mysql","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/mysql/logs","Destination":"/var/log/mysql","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/mysql/my.cnf","Destination":"/etc/mysql/mysql.conf.d/mysqld.cnf","Mode":"","RW":true,"Propagation":"rprivate"}]},{"Id":"2f4c41c883aadf679166e132a810ca6c4dc63f1aef2141275bb2d44beabfda12","Names":["/tokyo-v2ray"],"Image":"docker.io/v2ray/official","ImageID":"sha256:77469193c5291dcc15f5d6760f40f1935d73ba8f0bdfc8c7d67cf6084cec5cf4","Command":"v2ray --config=/etc/v2ray/tokyo-v2ray.json","Created":1634029878,"Ports":[],"Labels":{"maintainer":"Darian Raymond <admin@v2ray.com>"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"230407e121b589a4d80b08b647e70ba999be08390fa005ae718b147b59de6bab","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/v2ray/config","Destination":"/etc/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/v2ray/log","Destination":"/var/log/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"d40963b043b20e1086b0b9db62ff6f503ccbc3174155d543f26599fb5a53631f","Names":["/zhiweidata-v2ray"],"Image":"docker.io/v2ray/official","ImageID":"sha256:77469193c5291dcc15f5d6760f40f1935d73ba8f0bdfc8c7d67cf6084cec5cf4","Command":"v2ray --config=/etc/v2ray/zhiweidata.json","Created":1634027071,"Ports":[],"Labels":{"maintainer":"Darian Raymond <admin@v2ray.com>"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"c3f0481bf7a8a2384a40232446c471c952f1547da3c9b3627974ce2264379e66","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/v2ray/config","Destination":"/etc/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/v2ray/log","Destination":"/var/log/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"d1d8e5a84dc45e52d9df51645004c606aa0e96a56c9119376d78ebf3ab332403","Names":["/cucedu-push-channeldata"],"Image":"cucedu-push-channeldata","ImageID":"sha256:d0302126880f1aab8bbbc032f279e2db762b971d696228b2345b6a9210d7e878","Command":"java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512M -Xmx1G -jar cucedu-push-channeldata.jar","Created":1625648005,"Ports":[],"Labels":{},"State":"exited","Status":"Exited (143) 2 weeks ago","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"a6627d41463c4be87184cf29598b90bce10231e2304e39af01fd128e6aee079f","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[]}]

服务器是docker,这是请求失败的结果:

[{"Id":"9467b3bee76d8b7f4212ddff890e0c46334d4f181f51652871e1d1a047f43d3e","Names":["/barrage-crawler-server"],"Image":"barrage-crawler-server","ImageID":"sha256:ea42e0cd2ad4505fbdad4add7d40c9e9f79ab932804db12e43ec898ea1659032","Command":"java -Xmx4G -Xms4G -Xloggc:logs/gc.log -XX:+HeapDumpOnOutOfMemoryError -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=6000 -Dcom.sun.management.jmxremote.rmi.port=6000 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.119 -jar barrage-crawler-server.jar","Created":1634116299,"Ports":[{"IP":"0.0.0.0","PrivatePort":8080,"PublicPort":8080,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":6000,"PublicPort":6000,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":5005,"PublicPort":5010,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up 6 hours","HostConfig":{"NetworkMode":"static"},"NetworkSettings":{"Networks":{"static":{"IPAMConfig":{"IPv4Address":"192.168.98.2"},"Links":null,"Aliases":null,"NetworkID":"9f82c2510202a41a9ea489c6ba3626d2970808acf843fc36f3f4154910847e1d","EndpointID":"6d1cca5727b935ce78cc7c74b881c55e27b928df2d93ed4b4f16d6cbeb752834","Gateway":"192.168.98.1","IPAddress":"192.168.98.2","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:c0:a8:62:02"}}},"Mounts":[]},{"Id":"cfbc21516759c40372aba8e777bc0c12b0f7ff7b2be1c2494b28d03de8b8fc60","Names":["/automatic-test"],"Image":"automatic-test","ImageID":"sha256:83ddc4b1ab867ac917c098c434c09df9d11063e43d04e8a5bed2353675d84609","Command":"java -Xms512M -Xmx512M -jar automatic-test-jar-with-dependencies.jar","Created":1634113207,"Ports":[],"Labels":{},"State":"running","Status":"Up 7 hours","HostConfig":{"NetworkMode":"static"},"NetworkSettings":{"Networks":{"static":{"IPAMConfig":{"IPv4Address":"192.168.98.3"},"Links":null,"Aliases":null,"NetworkID":"9f82c2510202a41a9ea489c6ba3626d2970808acf843fc36f3f4154910847e1d","EndpointID":"e191fc43f4a598d96c721ec186dd778aee8efd8a404db7ef381ec29f03cc6029","Gateway":"192.168.98.1","IPAddress":"192.168.98.3","IPPrefixLen":24,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:c0:a8:62:03"}}},"Mounts":[]},{"Id":"967074c17f846d7aad0fc60329d6d57674f6715bc77fc6091f7f566487c5597e","Names":["/gitea"],"Image":"gitea/gitea","ImageID":"sha256:fb3d5df4d2978f50e0d6168029746739fc0c8110da8d6f8e3771deb715c3e093","Command":"/usr/bin/entrypoint /bin/s6-svscan /etc/s6","Created":1634055325,"Ports":[{"IP":"0.0.0.0","PrivatePort":3000,"PublicPort":3000,"Type":"tcp"},{"IP":"0.0.0.0","PrivatePort":22,"PublicPort":3001,"Type":"tcp"}],"Labels":{"maintainer":"maintainers@gitea.io","org.opencontainers.image.created":"2021-05-14T05:16:51Z","org.opencontainers.image.revision":"44286e29f0a96e0228ebca567b6694e7b2ffccd3","org.opencontainers.image.source":"https://github.com/go-gitea/gitea.git","org.opencontainers.image.url":"https://github.com/go-gitea/gitea"},"State":"running","Status":"Up 23 hours","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"a6627d41463c4be87184cf29598b90bce10231e2304e39af01fd128e6aee079f","EndpointID":"3145eacea9b6459bf22728afecc497d2bac2a59895b273946bb7762d10593910","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}},"Mounts":[{"Type":"volume","Name":"133e605b5bca962cd67f51fc0da7f3628fe7670d920f01213345e4bb7365962f","Source":"/var/lib/docker/volumes/133e605b5bca962cd67f51fc0da7f3628fe7670d920f01213345e4bb7365962f/_data","Destination":"/data","Driver":"local","Mode":"","RW":true,"Propagation":""},{"Type":"bind","Source":"/home/gitea/config","Destination":"/data/gitea","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/gitea/git","Destination":"/data/git","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"e2da15ab5ccfb97ad2080c84e964d435d0a32a8cecffdf581a8a31b3e9241822","Names":["/redis"],"Image":"redis","ImageID":"sha256:de974760ddb2f32dbddb74b7bb8cff4c1eee06d43d36d11bbca1dc815173916e","Command":"docker-entrypoint.sh redis-server","Created":1634032228,"Ports":[],"Labels":{},"State":"running","Status":"Up 29 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"41d494a74057dc669dd3ad8f411da552067e291f2410d321f014e30a537742e9","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/redis/data","Destination":"/data","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"f8c79216291d5905a2327f3a1cbbf02da9e5e6112c712c45f0e98b25f68c8111","Names":["/mongo"],"Image":"mongo","ImageID":"sha256:30b3be246e3953807bd3ec956b05f6018548de547f457fa00890e5d4684dc68a","Command":"docker-entrypoint.sh mongod","Created":1634032015,"Ports":[],"Labels":{},"State":"running","Status":"Up 29 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"4468d6246ad3397d0529ac068f30b2f324d8d3dd7e07f58efb18df8209620162","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/mongo/data","Destination":"/data/db","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"volume","Name":"377e5e25c83db03bebbadb8a556ee009cc64448dd9d80f39030b9dbd31bb337c","Source":"/var/lib/docker/volumes/377e5e25c83db03bebbadb8a556ee009cc64448dd9d80f39030b9dbd31bb337c/_data","Destination":"/data/configdb","Driver":"local","Mode":"","RW":true,"Propagation":""}]},{"Id":"5fca4c5aca0295d5661064229bb17643572637eafebc41d12348418720570d50","Names":["/elasticsearch"],"Image":"docker.elastic.co/elasticsearch/elasticsearch:7.9.2","ImageID":"sha256:caa7a21ca06ea2376dc2a2b3194795def7bed7cc1a4de18feee0a706518d2c39","Command":"/tini -- /usr/local/bin/docker-entrypoint.sh eswrapper","Created":1634031546,"Ports":[],"Labels":{"org.label-schema.build-date":"2020-09-23T00:45:33.626720Z","org.label-schema.license":"Elastic-License","org.label-schema.name":"Elasticsearch","org.label-schema.schema-version":"1.0","org.label-schema.url":"https://www.elastic.co/products/elasticsearch","org.label-schema.usage":"https://www.elastic.co/guide/en/elasticsearch/reference/index.html","org.label-schema.vcs-ref":"d34da0ea4a966c4e49417f2da2f244e3e97b4e6e","org.label-schema.vcs-url":"https://github.com/elastic/elasticsearch","org.label-schema.vendor":"Elastic","org.label-schema.version":"7.9.2","org.opencontainers.image.created":"2020-09-23T00:45:33.626720Z","org.opencontainers.image.documentation":"https://www.elastic.co/guide/en/elasticsearch/reference/index.html","org.opencontainers.image.licenses":"Elastic-License","org.opencontainers.image.revision":"d34da0ea4a966c4e49417f2da2f244e3e97b4e6e","org.opencontainers.image.source":"https://github.com/elastic/elasticsearch","org.opencontainers.image.title":"Elasticsearch","org.opencontainers.image.url":"https://www.elastic.co/products/elasticsearch","org.opencontainers.image.vendor":"Elastic","org.opencontainers.image.version":"7.9.2"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"04fa8af3719eae67d2d99c0b3216cef2c3392a7546f575f7d7ebd790d6f17378","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/elasticsearch/config","Destination":"/usr/share/elasticsearch/config","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/elasticsearch/data","Destination":"/var/lib/elasticsearch","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/elasticsearch/logs","Destination":"/var/log/elasticsearch","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"8c805aa531b49648440d06e0c124b37e054822c8c419fd25338277dd08881d58","Names":["/mysqld"],"Image":"mysql:5.7.25","ImageID":"sha256:98455b9624a96e32b353297bb312913b6bbd62ac195cea2c7dd477209ba572d6","Command":"docker-entrypoint.sh mysqld","Created":1634030551,"Ports":[],"Labels":{},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"f16d5a1abb34d1917197b5d0125541da147cdfeb90ec07f61a5e5b754a1c3a48","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/mysql/logs","Destination":"/var/log/mysql","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/mysql/my.cnf","Destination":"/etc/mysql/mysql.conf.d/mysqld.cnf","Mode":"","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/mysql/data","Destination":"/var/lib/mysql","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"2f4c41c883aadf679166e132a810ca6c4dc63f1aef2141275bb2d44beabfda12","Names":["/tokyo-v2ray"],"Image":"docker.io/v2ray/official","ImageID":"sha256:77469193c5291dcc15f5d6760f40f1935d73ba8f0bdfc8c7d67cf6084cec5cf4","Command":"v2ray --config=/etc/v2ray/tokyo-v2ray.json","Created":1634029878,"Ports":[],"Labels":{"maintainer":"Darian Raymond <admin@v2ray.com>"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"230407e121b589a4d80b08b647e70ba999be08390fa005ae718b147b59de6bab","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/v2ray/config","Destination":"/etc/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/v2ray/log","Destination":"/var/log/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"d40963b043b20e1086b0b9db62ff6f503ccbc3174155d543f26599fb5a53631f","Names":["/zhiweidata-v2ray"],"Image":"docker.io/v2ray/official","ImageID":"sha256:77469193c5291dcc15f5d6760f40f1935d73ba8f0bdfc8c7d67cf6084cec5cf4","Command":"v2ray --config=/etc/v2ray/zhiweidata.json","Created":1634027071,"Ports":[],"Labels":{"maintainer":"Darian Raymond <admin@v2ray.com>"},"State":"running","Status":"Up 30 hours","HostConfig":{"NetworkMode":"host"},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"14abf65805a3b9367d052172ace4c445b4c40d8d3e080b251c2092d98999dcce","EndpointID":"c3f0481bf7a8a2384a40232446c471c952f1547da3c9b3627974ce2264379e66","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[{"Type":"bind","Source":"/home/v2ray/config","Destination":"/etc/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/v2ray/log","Destination":"/var/log/v2ray","Mode":"z","RW":true,"Propagation":"rprivate"}]},{"Id":"d1d8e5a84dc45e52d9df51645004c606aa0e96a56c9119376d78ebf3ab332403","Names":["/cucedu-push-channeldata"],"Image":"cucedu-push-channeldata","ImageID":"sha256:d0302126880f1aab8bbbc032f279e2db762b971d696228b2345b6a9210d7e878","Command":"java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512M -Xmx1G -jar cucedu-push-channeldata.jar","Created":1625648005,"Ports":[],"Labels":{},"State":"exited","Status":"Exited (143) 2 weeks ago","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"a6627d41463c4be87184cf29598b90bce10231e2304e39af01fd128e6aee079f","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":""}}},"Mounts":[]}]a489A�

后面的“a489A�”是无用字节,导致我json解析失败。我该如何解决这个问题?

使用“正确的”C++,您的代码可以重写如下:

size_t get_containers_callback(const char *buff, size_t size, size_t buff_size, void *data)
{
    std::string* body = static_cast<std::string*>(data);
    body->append(buff, buff_size);
    return buff_size;
}

int main()
{
    // ... code to initialize the curl structure ...

    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/containers/json?all=true");

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_containers_callback);

    std::string body;
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);

    res = curl_easy_perform(curl);
    if (res == CURLE_OK)
    {
        std::cout << body << '\n';
    }

    // ... code to clean up ...
}