json 邮递员将表单数据转义到 API 导致意外令牌错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28369896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:44:09  来源:igfitidea点击:

Postman escaping form-data to API causing unexpected token error

jsonescapingpostman

提问by courtyen

I am trying to use Postman to troubleshoot an API error I am getting. I am using OAuth to authenticate and so I am posting headers and form-data. However, it seems the error I have is related to the form-data. I have also tried with the 'Content-Type' to be 'application/json; charset=utf-8' and without. Same results.

我正在尝试使用 Postman 来解决我遇到的 API 错误。我正在使用 OAuth 进行身份验证,因此我正在发布标头和表单数据。但是,我遇到的错误似乎与表单数据有关。我也试过将“内容类型”设为“应用程序/json;”。charset=utf-8' 和没有。结果一样。

{
    "error": "795: unexpected token at '------WebKitFormBoundaryoRBw9tLdltLHfrtU\r\nContent-   Disposition: form-data; name=\"topic\"\r\n\r\ncheckouts/update\r\n------WebKitFormBoundaryoRBw9tLdltLHfrtU\r\nContent-Disposition: form-data; name=\"address\"\r\n\r\nhttps://mysite.co/checkout/\r\n------WebKitFormBoundaryoRBw9tLdltLHfrtU\r\nContent-Disposition: form-data; name=\"format\"\r\n\r\njson\r\n------WebKitFormBoundaryoRBw9tLdltLHfrtU--\r\n'"
}

I think Postman is escaping my data before the API can read it in correctly. Any ideas how to get Postman to stop escaping my data prematurely? There seems to be no issues on it.

我认为 Postman 在 API 可以正确读取我的数据之前就对其进行了转义。任何想法如何让邮递员停止过早地转义我的数据?似乎没有问题。

Body of request:

请求正文:

"webhook":[{"topic": "checkouts/update","address": "https://mysite.co/checkout","format": "json"}]

回答by Laurent Jalbert Simard

You need to enclose your JSON property inside a JSON object, that's usually what APIs are expecting.

您需要将 JSON 属性包含在 JSON 对象中,这通常是 API 所期望的。

{ 
    "webhook":[
        {"topic": "checkouts/update","address": "https://mysite.co/checkout","format": "json"}
    ] 
}