基本思想:需要使用http协议完成业务需求,需要测试一下,所以学习一下想关的应用实践
一、下载Postman Postman
ubuntu@ubuntu:~$ tar-zxvf postman-linux-x64.tar.gz
ubuntu@ubuntu:~/postman-linux-x64/Postman$ ./Postman
帐号某宝解决,也可以试用30天
二、固定postman到任务栏图标
ubuntu@ubuntu:~$ sudo gedit /usr/share/applications/Postman.desktop
添加内容
[Desktop Entry]
Type=Application
Name=Postman
GenericName=Postman
Comment=Postman:The Postman IDE
Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
Terminal=Postman
Categories=Postman
再次设置一下图标。使用下列命令打开Postman然后用十字光标点击一下postman工具,显示下列字段
ubuntu@ubuntu:~$ xprop |grep WM_CLASS
WM_CLASS(STRING) = "postman", "Postman"
ubuntu@ubuntu:~$
重新修正配置文件
[Desktop Entry]
Type=Application
Name=Postman
GenericName=Postman
Comment=Postman:The Postman IDE
Exec="/home/ubuntu/postman-linux-x64/Postman/Postman" %f
Icon=/home/ubuntu/postman-linux-x64/Postman/app/icons/icon_128x128.png
Terminal=Postman
Categories=Postman
StartupWMClass=postman
再次重启Postman就可以添加任务栏,固定住
三、建立postman请求http服务测试,首先需要搭建nginx的http服务器,支持http服务,参考14、Android Studio通过http向C++服务端传递base64图片,然后对图片处理(写入本地)返回数据给Android studio_sxj731533730的博客-CSDN博客_c++ http传图片
发送成功,同时参考它的例子,将其代码抄下来,将https改成http,这样不用ssl支持,就可以在代码中集成使用了
四、系统好像默认支持curl,可以不用编译源码安装或者命令安装,跳过源码安装直接执行下面的事例子代码执行和测试
ubuntu@ubuntu:~$ sudo apt-get install curl
或者源码下载libcurl :curl downloads
ubuntu@ubuntu:~$ wget https://curl.se/download/curl-7.85.0.tar.gz
ubuntu@ubuntu:~$ tar -zxvf curl-7.85.0.tar.gz
ubuntu@ubuntu:~/curl-7.85.0$ ./configure --without-ssl
ubuntu@ubuntu:~/curl-7.85.0$ make
ubuntu@ubuntu:~/curl-7.85.0$ sudo make install
测试代码
cmakelists.txt
cmake_minimum_required(VERSION 3.16)
project(demo_curl)
set(CMAKE_CXX_STANDARD 14)
add_executable(demo_curl main.cpp)
target_link_libraries(demo_curl -lcurl )
源码
#include<stdio.h>
#include<curl/curl.h>
int main(int argc, char *argv[])
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "192.168.10.26:8334/api/hello");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Authorization: Basic dmNoYXdsYTpIZXJlQDExMTE=");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
测试结果
/home/ubuntu/demo_curk/cmake-build-debug/demo_curl
{ "result": welcome to httpserver }
Process finished with exit code 0
版权归原作者 sxj731533730 所有, 如有侵权,请联系我们删除。