前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过 Proxy 汉化 restful 接口

通过 Proxy 汉化 restful 接口

作者头像
netkiller old
发布2018-03-06 09:52:56
9240
发布2018-03-06 09:52:56
举报
文章被收录于专栏:NetkillerNetkiller

本文节选自《Netkiller Web 手札》

1.4.14.6. 通过 Proxy 汉化 restful 接口

通过 proxy 汉化 restful 接口返回的 json 字符串。

背景,有这样一个需求,前端HTML5通过ajax与restful交互,ajax会显示接口返回json数据,由于js做了混淆无法修改与restful交互的逻辑,但是json反馈结果需要汉化。

汉化前接口如下,返回message为 "message":"Full authentication is required to access this resource"

代码语言:javascript
复制
			neo@netkiller ~/workspace/Developer/Python % curl http://api.netkiller.cn/restful/member/get/1.json

{"timestamp":1505206067543,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/restful/member/get/1.json"}   

建立一个代理服务器,代理介于用户和接口之间,ajax 访问接口需要经过这个代理服务器中转。

增加 /etc/nginx/conf.d/api.netkiller.cn.conf 配置文件

代码语言:javascript
复制
			server {
	listen 80;
	server_name api.netkiller.cn;

	charset utf-8;
	
	location / {
		proxy_pass http://localhost:8443;
		proxy_http_version 1.1;
		proxy_set_header    Host    $host;

		sub_filter_types application/json; 
        sub_filter 'Full authentication is required to access this resource'  '用户验证错误';
        sub_filter_once off;
	}

}			

所谓汉化就是字符串替换,使用nginx sub_filter 模块。

重新启动 nginx 然后测试汉化效果

代码语言:javascript
复制
			neo@netkiller ~/workspace/Developer/Python % curl http://api.netkiller.cn/restful/member/get/1.json

{"timestamp":1505208931927,"status":401,"error":"Unauthorized","message":"用户验证错误","path":"/restful/member/get/1.json"}   
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.4.14.6. 通过 Proxy 汉化 restful 接口
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档