shell命令,curl调用接口中文乱码问题解决
shell命令,curl调用接口中文乱码问题解决
·
使用curl发送一个带中文参数的请求,接口收到后得到的数据乱码,如下:
请求命令脚本:
#!/bin/sh
MSG="中文字符"
# 调用massage校验接口
RESULT=`curl -H "Content-Type:application/x-www-form-urlencoded" -s -X POST -d "msg=$MSG" "http://localhost:8080/tt/test"`
echo $RESULT;
收到后中文乱码:
解决方案
发送请求之前先对字符进行urlEncode编码
MSG="中文字符"
# 进行urlencode编码
urlEncodeMsg=`echo -n "$MSG" | xxd -ps | tr -d '\n' | sed -r 's/(..)/%\1/g'`;
最终脚本如下:
#!/bin/sh
MSG="中文字符"
# 进行urlencode编码,然后传输,否则接口收到的中文是乱码
urlEncodeMsg=`echo -n "$MSG" | xxd -ps | tr -d '\n' | sed -r 's/(..)/%\1/g'`;
# 调用massage校验接口
RESULT=`curl -H "Content-Type:application/x-www-form-urlencoded" -s -X POST -d "msg=$urlEncodeMsg" "http://localhost:8080/tt/test"`
echo $RESULT;
更多推荐
已为社区贡献4条内容
所有评论(0)