NodeRed http request [“user-agent“]=“curl“

近日学习合宙luat 获取外网IP地址的程序,有一句通过GET请求https://ip.fm/网址获取IP地址的方法,

local function ip_prase()
	http.request("GET","https://ip.fm/",nil,{["user-agent"]="curl"},nil,120000,function (result,prompt,head,body)
	-- http.request("GET","www.baidu.com",nil,{["user-agent"]="curl"},nil,120000,function (result,prompt,head,body)
		log.info("ip",result,prompt,head,body)
		if result then
			local ip_s,addr_s = body:match("IP: *(%d+%.%d+%.%d+%.%d+) *来自: *(.+)")

			ip = ip_s or "未知ip"
			ip_r = addr_s or "未知地址"
			-- log.info("IP",string.format("%s",addr_s))
		else
			ip = "外网IP获取失败"
		end
	end)
end

官网关于发送HTTP请求说明

由官网文档可知请求后的数据返回到body中,于是用POSTMAN GET请求了https://ip.fm/,发现返回的数据是网页代码,如下:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>IP.FM - IP 查询</title>
  <meta name="description" content="IP 地址和归属地查询">
  <link rel="shortcut icon" href="icon.png">
  <link rel="icon" sizes="192x192"  href="icon.png">
  <link rel="apple-touch-icon-precomposed" href="icon.png">
  <meta name="viewport" content="width=device-width, minimum-scale=0.5, user-scalable=no">
  <link href='/css/main.css' rel='stylesheet' type='text/css'>
  <link href='/css/bootstrap.min.css' rel="stylesheet">
</head>
<body>
  <div class="container-fluid">
      <div class="header">
          <a href="/"><img src="/logo.png" width="200px" height="80px"></a>
      </div>
    <div class="searchform">
      <form name="fs" action="/" method="GET" class="form-search">
        <input name="ip" type="text" placeholder="请输入要查询的 IP 或域名" class="span3" autofocus>
        <input id="s" type="submit" class="btn btn-danger" value="查询">
      </form>
    </div>
    <div id="result"><div class="well text-left">
<p>你的 IP:<code>49.78.198.167</code></p><p> 来自:中国 江苏 电信</p>    </div>
  </div>
  <div class="footer">
    <p>&copy;2018-2021 IP.FM</p>  </div>
</body>
</html>

但通过air724开发板串口输出的只有画红线的部分,发现网页数据大部分被过滤掉了。

 后经请教合宙晨旭,知道是["user-agent"]="curl"起作用,根据["user-agent"]="curl"网站只返回了下面数据:

IP: 220.205.244.3 来自: 中国 安徽 淮南 联通

后直接在ubuntu上

curl https://ip.fm/

 结果的确只发回一串数据,说明网站根据["user-agent"]="curl"字段对返回数据进行了优化。

一 在POSTMAN上也可以实现上述功能,

 打开后,发现默认User-Agent是PostmanRuntime/7.29.0

 现在取消原有User-Agent,添加新的User-Agent,并设置为curl

 然后发送GET请求,得到如下返回数据:

二 用NodeRed 实现:

参考NodeRed官方文档

返回数据

 

 函数节点设置

msg.headers = {};
msg.headers["user-agent"]="curl"
return msg;

http-request节点

 完整流图代码:

[{
	"id": "1bece4fc39248d8c",
	"type": "tab",
	"label": "流程 12",
	"disabled": false,
	"info": "",
	"env": []
}, {
	"id": "4ff902257797dedc",
	"type": "inject",
	"z": "1bece4fc39248d8c",
	"name": "",
	"props": [{
		"p": "payload"
	}, {
		"p": "topic",
		"vt": "str"
	}],
	"repeat": "",
	"crontab": "",
	"once": false,
	"onceDelay": 0.1,
	"topic": "",
	"payload": "",
	"payloadType": "date",
	"x": 250,
	"y": 280,
	"wires": [
		["9b2e0f733e58db57"]
	]
}, {
	"id": "22db19125d0af4ee",
	"type": "debug",
	"z": "1bece4fc39248d8c",
	"name": "",
	"active": true,
	"tosidebar": true,
	"console": false,
	"tostatus": false,
	"complete": "false",
	"statusVal": "",
	"statusType": "auto",
	"x": 730,
	"y": 280,
	"wires": []
}, {
	"id": "5105e505b74da0c8",
	"type": "http request",
	"z": "1bece4fc39248d8c",
	"name": "",
	"method": "GET",
	"ret": "txt",
	"paytoqs": "ignore",
	"url": "https://ip.fm/",
	"tls": "",
	"persist": false,
	"proxy": "",
	"authType": "",
	"senderr": false,
	"credentials": {},
	"x": 540,
	"y": 280,
	"wires": [
		["22db19125d0af4ee"]
	]
}, {
	"id": "9b2e0f733e58db57",
	"type": "function",
	"z": "1bece4fc39248d8c",
	"name": "",
	"func": "msg.headers = {};\nmsg.headers[\"user-agent\"]=\"curl\"\nreturn msg;",
	"outputs": 1,
	"noerr": 0,
	"initialize": "",
	"finalize": "",
	"libs": [],
	"x": 390,
	"y": 280,
	"wires": [
		["5105e505b74da0c8"]
	]
}]

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

armcsdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值