22 Star 67 Fork 23

xiaozhuai / cxxurl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
example_post.cpp 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
xiaozhuai 提交于 2018-01-23 23:05 . optimize code for creating form
/**
* @author : xiaozhuai
* @date : 17/1/4
*/
#include <iostream>
#include <sstream>
#include <cxxurl/cxxurl_all.h>
using namespace std;
using namespace CXXUrl;
void example_simple_form();
void example_multipart_form();
void example_raw_body_text();
void example_raw_body_binary();
int main(int argc, char** argv){
example_simple_form();
example_multipart_form();
example_raw_body_text();
example_raw_body_binary();
}
void example_simple_form(){
ostringstream contentOutput;
// simple form, you can only set key-value (x-www-form-urlencoded)
SimpleForm form;
form.add("name", "xiaozhuai");
form.add("sex", "male");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_multipart_form(){
ostringstream contentOutput;
// multipart form, you can upload a file, or key-value
MultipartForm form;
form.add("name", "xiaozhuai");
form.addFile("avatar", "../example_server/tao.png");
form.addFile("avatar2", "../example_server/tao.png", "tao2.png");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_raw_body_text(){
ostringstream contentOutput;
// raw form, you can set request body with text
RawRequestBody form;
form.setRawText("{ \"name\": \"xiaozhuai\" }");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentType("application/json")
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_raw_body_binary(){
ostringstream contentOutput;
// raw form, you can set request body with raw bytes
char buffer[100];
RawRequestBody form;
form.setRawData(buffer, 100);
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentType("application/octet-stream")
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
C++
1
https://gitee.com/xiaozhuai/cxxurl.git
git@gitee.com:xiaozhuai/cxxurl.git
xiaozhuai
cxxurl
cxxurl
master

搜索帮助