如何在 Jenkinsfile 中正确转义用于curl 调用的 JSON 字符串?
我正在编写 Jenkinsfile,并在使用 CURL 命令调用 API 的阶段中间,我有类似的内容:
sh '''
accept_hdr="Accept:*/*"
fetch_mode_hdr="Sec-Fetch-Mode: cors"
content_type_hdr="Content-Type: application/json"
currentMonth="03"
currentYear="2022"
studentID="12345"
data="[{"month":"${currentMonth}","year":"${currentYear}","id":"${studentID}"}]"
curl -k -v --include --header "$accept_hdr" --header "$content_type_hdr" --header "$fetch_mode_hdr" --data "$data" -X POST http://testurl:555/path/api/${studentID}
'''
当我检查我的日志时,它显示以下内容:
curl -k -v --include --header 'Accept:*/*' --header 'Content-Type: application/json' --header 'Sec-Fetch-Mode: cors' --data '[{currentMonth:03,currentYear:2022,studentID: 12345}]' -X POST http://testurl:555/path/api/12345
它给了我一个错误说:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('c' (code 114)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('c' (code 114)): was expecting double-quote to start field name
我看到这是因为它正在将其发送为 [{currentMonth:03,currentYear:2022,studentID:12345}]
而不是 [{"currentMonth":"03","currentYear":"2022","studentID":"12345"}]
所以我尝试在线转义字符串并使用它: [{\"月\":\"${currentMonth}\",\"年\":\"${currentYear}\",\"id\":\"${studentID}\"} ]
但仍然存在相同的错误,并且格式仍然与以前相同,没有双引号。
不知道如何解决这个问题,我已经被困了好几个小时了,试图以不同的方式格式化它。
I am writing a Jenkinsfile and in the middle of a stage to call an API with a CURL command, I have something like:
sh '''
accept_hdr="Accept:*/*"
fetch_mode_hdr="Sec-Fetch-Mode: cors"
content_type_hdr="Content-Type: application/json"
currentMonth="03"
currentYear="2022"
studentID="12345"
data="[{"month":"${currentMonth}","year":"${currentYear}","id":"${studentID}"}]"
curl -k -v --include --header "$accept_hdr" --header "$content_type_hdr" --header "$fetch_mode_hdr" --data "$data" -X POST http://testurl:555/path/api/${studentID}
'''
When I check my logs, it's showing this:
curl -k -v --include --header 'Accept:*/*' --header 'Content-Type: application/json' --header 'Sec-Fetch-Mode: cors' --data '[{currentMonth:03,currentYear:2022,studentID: 12345}]' -X POST http://testurl:555/path/api/12345
It's giving me an error saying:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('c' (code 114)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('c' (code 114)): was expecting double-quote to start field name
I see that this is because it is sending it as[{currentMonth:03,currentYear:2022,studentID: 12345}]
instead of [{"currentMonth":"03","currentYear":"2022","studentID":"12345"}]
So I tried to escape the string online and used this instead: [{\"month\":\"${currentMonth}\",\"year\":\"${currentYear}\",\"id\":\"${studentID}\"}]
but there was still the same error and it still was in the same format as before, without the double quotes.
Any idea how I can resolve this, I've been stuck for hours now trying to format it differently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论