用到的库:
假设现在 Client 想要获取 Person 喜欢的语言,需要发送 Person 的个人信息到 Server,Server 根据用户姓名返回他喜欢的语言。
struct Book {
std::string name;
struct Person {
std::string name;
int age;
std::vector<Book> favoriteBooks;
struct FavoriteLanguage {
std::string name;
std::vector<std::string> languages;
这个过程需要三个处理步骤:
- Client 将 Person 信息转为 json,并通过 curl 发送出去
- Server 收到数据后解 json 处理并返回该 Person 喜欢的语言
- Client 收到 Response 后解 json 得到喜欢的语言
Book 转 json:
void to_json(json &j, const Book &book) {
j = {
{"name", book.name}
Person 转 json:
void to_json(json &j, const Person &p) {
j = {
{"name", p.name},
{"age", p.age}
if (!p.favoriteBooks.empty())
j.push_back({"books", p.favoriteBooks});
Person p{"Aland", 18, books};
json j = p;
std::string dump = j.dump();
通过 curl 发送 http POST 请求,数据为 json 格式。
std::string url = "127.0.0.1:9999/favorbooks";
CURL *curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dump.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &favor);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
Client 解析返回的 response:
static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
auto j = json::parse(std::string((char *) ptr));
FavoriteLanguage *favor = (FavoriteLanguage *) userp;
*favor = j;
return realsize;
json 转结构体:
void from_json(const json &j, FavoriteLanguage &favor) {
favor.name = j["name"];
favor.languages = j["languages"].get<std::vector<std::string>>();
解析后的打印结果:
{"age":18,"books":[{"name":"TCP/IP Illustrated, Volume 1"},{"name":"Advanced Programming in the UNIX Environment"}],"name":"Aland"}
{"languages":["C++","Golang","Python"],"name":"Aland"}
Name:Aland
Languages:
Golang
Python
注: 详细代码见 https://github.com/alandtsang/cppdemo/tree/master/src/curljson
遇到的错误:
could not find to_json() method in T's namespace
这个问题通常是两点引起的(下列解法对to_json
和 from_json
都适用):
- 没有对要转换的结构体做 to_json 转换。
- 外层结构体做了 to_json 转换,但是结构体包含的结构体没有做 to_json 转换。
C libcurl get output into a string
creating nested json object in c++ using nlohmann json
Unable to locate ‘to_json()’ and ‘from_json()’ methods in the same namespace
Why not also look inside the type for (static) to_json and from_json funtions?
用到的库:curlnlohmann/json假设现在 Client 想要获取 Person 喜欢的语言,需要发送 Person 的个人信息到 Server,Server 根据用户姓名返回他喜欢的语言。struct Book { std::string name;};struct Person { std::string name; int age; std:...
#include <curl.h>
#include <memory>
static size_t string_write(char *ptr, size_t size, size_t nmemb, std::string &str)
size_t total = size * nmemb;
if (t...
#include <jsoncpp/json/json.h>
#include <jsoncpp/json/reader.h>
#include <curl/curl.h>
using namespace std;
int upload(string url, string strData)
CURL *curl = NULL;
struct curl_slist *headers = NULL.
size_t getUrlResponse(void *buffer, size_t size, size_t count, void *response)
string *str = (string*)response;
(*str).append((char*)buffer, size*count);
return size * count;
//post请求
string upload(string url, string data)
string r.
转载:C++通过HTTP请求Get或Post方式请求Json数据
最近在工作中,由于合作商只提供uRL,我这边需要通过HTTP请求Get或Post方式请求Json数据,然后解析JSON格式,解析json我使用的第三方库jsoncpp,代码如下
#pragma once
#include <iostream>
#include <windows.h>
#include <wininet.h>
using namespace std;
//每次读取的字节数
#define
curl -i -X POST -H 'Content-type':'application/json' -d {BTime:$btime} http://api.baidu.com
变量没有做解析
原来在shell中,”” ‘还是有很大区别的,
把修改后的curl发送贴出
atime=`date -d $1 +%s`
btime=`date -d $2 +%s`
curl -i -X POST -H 'Content-type':'applic
复制代码 代码如下:$data = array(“name” => “Hagrid”, “age” => “36”);$data_string = json_encode($data);
$ch = curl_init(‘http://api.local/rest/users’);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);curl_setopt($ch
使用curl发送json格式的post请求的命令格式如下:
curl -H "Content-Type: application/json" -X POST -d 'json_data' http://your_url
其中,json_data是你需要发送的json数据,http://your_url是请求的url地址。
比如发送以下json数据:
"name":"John Smith",
"email":"john@example.com"
对应的curl命令为:
curl -H "Content-Type: application/json" -X POST -d '{"name":"John Smith","email":"john@example.com"}' http://your_url