如何在 Jenkinsfile 中正确转义用于curl 调用的 JSON 字符串?

发布于 2025-01-16 06:17:10 字数 1534 浏览 2 评论 0原文

我正在编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文