大家好,欢迎来到IT知识分享网。
工具说明
使用方式说明:
使用 nodejs搭建httpserver
简洁的参考
let http = require("http"); let https = require("https"); let path = require('path'); const fs = require('fs'); let querystring = require('querystring'); let server = http.createServer(); //根据项目的路径导入生成的证书文件 var privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.pem'), 'utf8'); var certificate = fs.readFileSync(path.join(__dirname, './certificate/ca.cer'), 'utf8'); var credentials = {
key: privateKey, cert: certificate}; let httpsServer = https.createServer(credentials); httpsServer.on("request",function (req,res) {
res.writeHead(201); //res.end("hello robin\n"); //创建空字符叠加数据片段 var data = ''; req.on('data',function (chunk) {
data += chunk; }); req.on("end",function () {
//这里是自己收到的数据 data = decodeURI(data); let parse = JSON.parse(data); const dataObject = querystring.parse(data); console.log(dataObject); console.log("received over:"); var t_rtn = {
"challenge":parse.content.challenge}; var t_rtnStr = JSON.stringify(t_rtn); console.log(t_rtnStr); res.end(t_rtnStr); }); }).listen(12121); server.on("request",function (req,res) {
// res.writeHead(200,{"Content-Type":"text",a:"hello robin"}); res.setHeader("Content-Type","text"); res.statusCode = 200; res.sendDate = true; //创建空字符叠加数据片段 var data = ''; req.on('data',function (chunk) {
data += chunk; }); req.on("end",function () {
//这里是自己收到的数据 data = decodeURI(data); const dataObject = querystring.parse(data); console.log(dataObject); console.log("received over:"); res.end("this is over. robin"); }); console.log("req.method:" + req.method); console.log(req.headers); console.log(req.content); console.log(req.url); console.log("===================="); res.write("hello robin"); // res.end("hello tiancai"); }); // server.listen(12121,function () {
// console.log("Server start 12121"); // });
nodejs发送请求
var http = require('http'); let https = require('https'); var querystring = require('querystring'); var post_data = {
a: 123, time: new Date().getTime()};//这是需要提交的数据 var post_data2 = {
event:'verify_webhook', client_key:'robin_client_key', content: {
change:12346 } } var content = querystring.stringify(post_data); var content2 = querystring.stringify(post_data2); var options = {
// hostname: 'agent.linkallchina.net', hostname: '113.244.23.57', port: 12121, path: 'test/index', method: 'POST', headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }; function readJSONResponse(response) {
var responseData = ''; response.on('data', function (chunk) {
responseData += chunk; }); response.on('end', function () {
var dataObj = JSON.parse(responseData); console.log("Raw Response: " +responseData); console.log("Message: " + dataObj.message); console.log("Question: " + dataObj.question); }); } var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) {
console.log('BODY: ' + chunk); //JSON.parse(chunk) }); }); req.on('error', function (e) {
console.log('problem with request: ' + e.message); }); // write data to request body //req.write(content); req.write(content2); req.end();
curl 发送请求
post 请求
curl -H "Content-Type:application/json" -H "Data_Type:msg" -X POST --data '{"event":"verify_webhook","client_key":"","content":{"challenge":12345}}' https://122.243.25.75:12121/tiancaiPath -k
-k 的意思,是因为我用了自己构造的证书,会导致认证警告。加上-k就没事了参考
-H 指定headers头的时候必须单个使用,即一个-H指定一个头字段信息
参考
postman 使用
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/129414.html