转义字符串中的单引号,并将该字符串插入另一个带有未转义单引号的字符串中

时间:2018-05-11 13:14:44

标签: ruby string curl escaping

试图让卷毛工作

这是红宝石代码

msg = "john's message"

@body = %Q|{"body":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"#{msg}"}]}]}}|.gsub("'"){"\\'"}

puts @body

curl_call = %Q|curl -X POST -H 'Content-Type: application/json' -H 'Authorization: Bearer xxxx' -d '#{@body}' --url 'https://api.atlassian.com/site/xxxx/conversation/xxxx/message'|

puts curl_call

system(curl_call)

这是错误:sh: 1: Syntax error: Unterminated quoted string

对建议开放

1 个答案:

答案 0 :(得分:0)

当您使用unicode编码(')替换\u0027时,它可以正常工作:

.gsub("'", "\u0027")