【问题标题】:Escape single quote in string and interpolate that string into another string with unescaped single quotes转义字符串中的单引号并将该字符串插入另一个带有未转义单引号的字符串
【发布时间】:2018-05-11 13:14:43
【问题描述】:

试图让 curl 调用来工作

这是红宝石代码

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

接受建议

【问题讨论】:

    标签: ruby string curl escaping


    【解决方案1】:

    当您将 ' 替换为 unicode 编码 (\u0027) 时,它会起作用:

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

    【讨论】:

    • 当我直接做时我不工作。你能解释一下它是如何工作的吗?
    • 我的回答没有用?我刚刚对其进行了测试,效果很好:(
    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 2023-04-02
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多