简体   繁体   English

将日志文件转义为 json 字符串,用于使用 Python 调用 REST

[英]Escape log file as a json string for use in REST call using Python

I have a log file with all kind of character that need escaping, quote, double quote, forward and backward slashes, URL, other jsons.我有一个日志文件,其中包含需要 escaping、引号、双引号、正斜杠和反斜杠、URL、其他 json 的各种字符。

I want to send this file as part of REST request which is in JSON format, how do I escape my file in Python to be ready for transporting.我想将此文件作为 JSON 格式的 REST 请求的一部分发送,如何转义 Python 格式的文件以准备传输。

Example of my file:我的文件示例:

(12/5/2020 8:20:39 PM) My Project: Log directory created
(12/5/2020 8:20:43 PM) My Project: SF auth result: {vOPDetail,},{vOPResponseCode,},{vOPState,success},{vOutputToken,00D4W},{vOpResponseBody,},{vOPType,},{vOPInstanceUrl,https://test.my.salesforce.com}
(12/5/2020 8:20:43 PM) My Project: Token: "00D4W0000"
(12/5/2020 8:20:43 PM) My Project: URL: https://test.my.salesforce.com

My REST call look like this:我的 REST 调用如下所示:

PATCH https://test.my.salesforce.com/services/data/v49.0/sobjects/My_Object/a2P4W00000272ujUAA

Body

{
    "My_Namespace__My_Object__c" : "$Log$"
}

Where $Log$ is where the log file will go其中 $Log$ 是日志文件的位置 go

The actual REST call is in another language, I just need PYTHON to escape the log file to be ready for transport.实际的 REST 调用是另一种语言,我只需要 PYTHON 转义日志文件以准备传输。 Here is what I tried and does NOT work:这是我尝试过但不起作用的方法:

import json
def EscapeJSONString(input):
    return json.dumps(input)

The input here is the entirely of the log file which is read using a different language, due to my circumstances I can not escape the log file in the other language, I can just read it as is.这里的输入完全是使用不同语言读取的日志文件,由于我的情况,我无法转义其他语言的日志文件,我可以按原样读取。

The other language just call this python function EscapeJSONString and provide the log as input and then save the output to a variable.另一种语言只需调用此 python function EscapeJSONString 并提供日志作为输入,然后将 output 保存到变量中。

Jumping from a language to the other just to escape a string doesn't sound like the best solution.从一种语言跳到另一种只是为了逃避一个字符串听起来不是最好的解决方案。 Transfering a large log file this way also seems very inefficient, why not skip REST for this specific call and have an HTTP request to get the log in it's raw format.以这种方式传输大型日志文件似乎也非常低效,为什么不跳过 REST 进行此特定调用并发出 HTTP 请求以获取原始格式的日志。

But if you want to do it this way, json.dumps() does the escaping you need for string variables as values, so you can build en entire json like so:但是,如果您想这样做, json.dumps()会执行您需要将字符串变量作为值的 escaping ,因此您可以像这样构建整个 json :

import json
def EscapeJSONString(input):
    return json.dumps({"My_Namespace__My_Object__c": input})

It's hard to tell what input is here, but you need to mention how this function actually gets executed, because the file content does get properly escaped if read directly in Python很难说出这里的input是什么,但你需要提到这个 function 是如何实际执行的,因为如果直接在 Python 中读取文件内容确实会正确转义

Note: Forward slashes and brackets don't need escaped注意:正斜杠和括号不需要转义

>>> import json
>>> with open('/tmp/data.txt') as f:
...     print(json.dumps({"log": f.read()}))
... )
...
{"log": "(12/5/2020 8:20:39 PM) My Project: Log directory created\n(12/5/2020 8:20:43 PM) My Project: SF auth result: {vOPDetail,},{vOPResponseCode,},{vOPState,success},{vOutputToken,00D4W},{vOpResponseBody,},{vOPType,},{vOPInstanceUrl,https://test.my.salesforce.com}\n(12/5/2020 8:20:43 PM) My Project: Token: \"00D4W0000\"\n(12/5/2020 8:20:43 PM) My Project: URL: https://test.my.salesforce.com\n"}

Assuming input is a string, then you'd be returning a string, not a JSON object for the HTTP body假设input是一个字符串,那么您将返回一个字符串,而不是 HTTP 主体的 JSON object

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

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