【问题标题】:Accepting new line character in POST request在 POST 请求中接受换行符
【发布时间】:2021-05-15 21:08:56
【问题描述】:

我正在从第三方获取 JSON 作为 POST 请求,我目前在 Spring Boot 中映射到我的对象。当任何属性具有换行符时,此调用将失败,因为它没有被转义。我该如何接受这个 JSON。 示例:

{
"name":"xyz",
"age":32,
"comment": "Hello, this is 
an example comment of how I am getting the JSON"
}

【问题讨论】:

    标签: java json spring-boot rest post


    【解决方案1】:

    当任何属性包含换行符时,此调用将失败,因为它没有被转义。

    应该失败 - JSON strings 中不允许使用未转义的换行符。如果 HTTP 请求有

    Content-Type: application/json
    

    那么你这里有一个格式错误的请求,服务器应该返回400 Bad Request,而不是尝试修复文档。

    cat <<JSON | jq .
    {
    "name":"xyz",
    "age":32,
    "comment": "Hello, this is
    an example comment of how I am getting the JSON"
    }
    JSON
    
    parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 5, column 48
    

    cat <<JSON | jq -r .comment
    {
    "name":"xyz",
    "age":32,
    "comment": "Hello, this is \nan example comment of how I am getting the JSON"
    }
    JSON
    
    Hello, this is 
    an example comment of how I am getting the JSON
    

    【讨论】:

    • 没错,但第三方软件需要修复,但与此同时,我需要能够处理这些值
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2018-11-13
    • 2017-01-30
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多