在JSON中包括后斜线
我正在尝试发布一个JSON有效载荷,其中包括单个后挡板。
预期的JSON是:
{"ID" : "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID" : "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type" : "FILL_LVL",
"MeasureTimestamp" : "\/Date(1656951856000+0000)\/",
"CreationTimestamp" : "\/Date(1656951852000+0000)\/",
"Value" : "14.000000"}
但是发送的JSON是:
{"ID": "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID": "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type": "FILL_LVL",
"MeasureTimestamp": "\\/Date(1656951856000+0000)\\/",
"CreationTimestamp": "\\/Date(1656951852000+0000)\\/",
"Value": "14.000000"}
因此被拒绝。
我的代码看起来像:
import json
import pyodata
import requests
import paho.mqtt.client as mqtt
# Connection to on prem SAP via Cloud Integration API
SERVICE_URL = '''XXX'''
HEADER = {'apikey': 'XXX'}
payload = {
"ID" : "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID" : "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type" : "FILL_LVL",
"MeasureTimestamp" : "\/Date(1656951856000+0000)\/",
"CreationTimestamp" : "\/Date(1656951852000+0000)\/",
"Value" : "14.000000"
}
session = requests.Session()
session.headers.update(HEADER)
response = session.head(SERVICE_URL, headers={'x-csrf-token': 'fetch'})
token = response.headers.get('x-csrf-token', '')
session.headers.update({'x-csrf-token': token})
x = session.post(SERVICE_URL, data = json.dumps(payload))
print(x.text)
如何实现适当的有效负载格式?
更新:
我实际上不需要包含BackSlash字符(\
),但邮政请求仍然失败。从Postman发送它的工作正常。
从Postman Works发送此消息,但返回“数据服务请求无法根据畸形语法理解”时,当我使用JSON.DUMPS()从Python发送它时:
{
"ID": "f760caa6-a54b-1eec-bef6-b7292029848f",
"DeviceID": "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type": "FILL_LVL",
"MeasureTimestamp": "/Date(1656951856000+0000)/",
"CreationTimestamp": "/Date(1656951852000+0000)/",
"Value": "14.000000"}
在分析SAP后端中的日志时,这两个请求主体看起来都相同。 ..
I am trying to post a JSON Payload that includes a single backslash.
The expected JSON is:
{"ID" : "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID" : "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type" : "FILL_LVL",
"MeasureTimestamp" : "\/Date(1656951856000+0000)\/",
"CreationTimestamp" : "\/Date(1656951852000+0000)\/",
"Value" : "14.000000"}
But the the JSON being send is:
{"ID": "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID": "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type": "FILL_LVL",
"MeasureTimestamp": "\\/Date(1656951856000+0000)\\/",
"CreationTimestamp": "\\/Date(1656951852000+0000)\\/",
"Value": "14.000000"}
Therefore it gets rejected.
My code looks like this:
import json
import pyodata
import requests
import paho.mqtt.client as mqtt
# Connection to on prem SAP via Cloud Integration API
SERVICE_URL = '''XXX'''
HEADER = {'apikey': 'XXX'}
payload = {
"ID" : "f760caa6-a54b-1eec-bef6-b7292029844f",
"DeviceID" : "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type" : "FILL_LVL",
"MeasureTimestamp" : "\/Date(1656951856000+0000)\/",
"CreationTimestamp" : "\/Date(1656951852000+0000)\/",
"Value" : "14.000000"
}
session = requests.Session()
session.headers.update(HEADER)
response = session.head(SERVICE_URL, headers={'x-csrf-token': 'fetch'})
token = response.headers.get('x-csrf-token', '')
session.headers.update({'x-csrf-token': token})
x = session.post(SERVICE_URL, data = json.dumps(payload))
print(x.text)
How can I achieve the proper payload formatting?
Update:
I do not actually need to include the backslash character (\
) but the POST request still fails. Sending it from Postman works fine.
Sending this from Postman works, but returns "The Data Services Request could not be understood due to malformed syntax" when I send it from python using json.dumps():
{
"ID": "f760caa6-a54b-1eec-bef6-b7292029848f",
"DeviceID": "f760caa6-a54b-1eec-bef6-a9fe0249a440",
"Type": "FILL_LVL",
"MeasureTimestamp": "/Date(1656951856000+0000)/",
"CreationTimestamp": "/Date(1656951852000+0000)/",
"Value": "14.000000"}
When analysing the logs in the SAP Backend both request bodies look identical though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最终,解决问题的答案非常微不足道。它是由python脚本中缺少的
'content-type':'application/json'
引起的。与Postman一起发送它是因为我想这是自动设置的。The answer to the problem was very trivial in the end. It was caused by a missing
'content-type': 'application/json'
in the Python script. Sending it with Postman worked because I guess this is automatically set.您可以用骇人听闻的方式来做:
You can do it in a hacky way:
json.dumps(有效载荷)
自动逃脱JSON字符串中的后斜线是正确的现象(参见第7节“ rel =” nofollow https://datatracker.ietf.org/doc/html/rfc7159#section-7 )noreferrer“ > nofollow noreferrer“>
,
因此可以在端点中使用JSON SPEC并解决/deserialize/deserialize/正确地删除JSON有效载荷。
The phenomenon that
json.dumps(payload)
automatically escapes the backslash in the JSON string is correct (Cf. https://datatracker.ietf.org/doc/html/rfc7159#section-7):So it is up to the application in the endpoint to apply the JSON spec and resolve/deserialize/unmarshal the JSON payload correctly.