64、ubuntu20.04安装Postman测试http通信和测试其libcurl支持http客户端发送request
基本思想:需要使用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就可以添加任务栏,固定住
发送成功,同时参考它的例子,将其代码抄下来,将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
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhebjjff
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13