简体   繁体   English

在我的 Python API POST 请求中使用替换时 POST 失败

[英]POST fails when using replace in my Python API POST request

I have a JSON dump that I am trying to post to a another application.我有一个 JSON 转储,我试图将其发布到另一个应用程序。 In the payload there is a "pageGroups": "['TEST_TCIS:Oncall Schedule']", and the double quotes need to be removed to represent this output to pass the data: "pageGroups": ['TEST_TCIS:Oncall Schedule'],在payload中有一个"pageGroups": "['TEST_TCIS:Oncall Schedule']",需要去掉双引号代表这个output传递数据: "pageGroups": ['TEST_TCIS:Oncall Schedule'],

I have tried to use replace but it fails.我曾尝试使用replace ,但它失败了。 If I manually remove the double quotes out of the JSON payload it works.如果我手动从 JSON 有效负载中删除双引号,它就可以工作。

Here is the code:这是代码:

api_url_base = "some url"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer some token"
     }
data = {"abendCode": "",
        "action": "CREATE",
        "assignGroup": "TCIS-APP-SUPPORT",
        "component": "tcis",
        "description": "Test incident creation",
        "entered": "", "logicalName": "tcis",
        "pageGroups": "['TEST_TCIS:Oncall Schedule']",
        "priority": "",
        "racfId": "",
        "sendPage": "",
        "sysProdName": "tcis",
        "transactionId": "",
        "type": "SYSMAN"}

data = json.dumps(data)
data = data.replace("\"[", "[").replace("]\"", "]")
print(data)


response = requests.post("https://apistaging.csx.com/tcis-events/v1/events",
                         json=data, headers=headers)
response_url = json.loads(response.content.decode('utf-8'))
print(response_url)
print("response " + str(response))

Here is the output I am getting:这是我得到的 output:

{"abendCode": "",
 "action": "CREATE",
 "assignGroup": "TCIS-APP-SUPPORT",
 "component": "tcis",
 "description": "Test incident creation",
 "entered": "",
 "logicalName": "tcis",
 "pageGroups": ['TEST_TCIS:Oncall Schedule'],
 "priority": "",
 "racfId": "",
 "sendPage": "",
 "sysProdName": "tcis",
 "transactionId": "",
 "type": "SYSMAN"}

{'traceId': 'd9dcd6ee-a0f3-170c-83e5-a52f2133cd31', 'error': {'code': 'APG400', 'message': 'The server could not process the request.'}}
response <Response [400]>
[Finished in 0.789s]

When you remove the double quotes from the already JSON-formatted data you get ['TEST_TCIS:Oncall Schedule'] , but this isn't valid JSON anymore.当您从已经采用 JSON 格式的数据中删除双引号时,您会得到['TEST_TCIS:Oncall Schedule'] ,但这不再是有效的 JSON。 If this is supposed to be a list containing one string, the string needs to be quoted with double quotes ( " ) in JSON.如果这应该是一个包含一个字符串的列表,则该字符串需要在 JSON 中用双引号 ( " ) 引起来。

To do this properly, remove the quotes from the value first, and then convert it to a JSON string.要正确执行此操作,请先从值中删除引号,然后将其转换为 JSON 字符串。

In fact, "removing the quotes" from "['TEST_TCIS:Oncall Schedule']" means to parse a string representation of a list into a list, for which there is ast.literal_eval .实际上, "['TEST_TCIS:Oncall Schedule']"中的“删除引号”意味着将列表的字符串表示解析为列表,其中有ast.literal_eval

import ast
import json

data = {"abendCode": "",
        "action": "CREATE",
        "assignGroup": "TCIS-APP-SUPPORT",
        "component": "tcis",
        "description": "Test incident creation",
        "entered": "", "logicalName": "tcis",
        "pageGroups": "['TEST_TCIS:Oncall Schedule']",
        "priority": "",
        "racfId": "",
        "sendPage": "",
        "sysProdName": "tcis",
        "transactionId": "",
        "type": "SYSMAN"}

data['pageGroups'] = ast.literal_eval(data['pageGroups'])
data = json.dumps(data)
print(data)

#response = requests.posts(..., json=data, ...)

Output (line breaks added manually): Output(手动添加换行符):

{"abendCode": "",
 "action": "CREATE",
 "assignGroup": "TCIS-APP-SUPPORT",
 "component": "tcis",
 "description": "Test incident creation",
 "entered": "",
 "logicalName": "tcis",
 "pageGroups": ["TEST_TCIS:Oncall Schedule"],
 "priority": "",
 "racfId": "",
 "sendPage": "",
 "sysProdName": "tcis",
 "transactionId": "",
 "type": "SYSMAN"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM