在JSON中包括后斜线

发布于 2025-02-13 20:21:36 字数 4302 浏览 1 评论 0原文

我正在尝试发布一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2025-02-20 20:21:36

最终,解决问题的答案非常微不足道。它是由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.

浅黛梨妆こ 2025-02-20 20:21:36

您可以用骇人听闻的方式来做:

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).replace('/', r'\/'))                                                     
print(x.text)

You can do it in a hacky way:

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).replace('/', r'\/'))                                                     
print(x.text)
偏爱自由 2025-02-20 20:21:36

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):

enter image description here

So it is up to the application in the endpoint to apply the JSON spec and resolve/deserialize/unmarshal the JSON payload correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文