为什么我的json.loads()调用的字符串格式不正确?

时间:2019-08-06 20:00:02

标签: python json python-requests

我正在为Cortex / Hive构建自定义响应器,但无法获取请求响应以正确转换为JSON格式。在本地开发环境中进行测试时,我的getCount函数中的代码可以完美地工作,但是当向其添加cortex responseer包装器时,我的代码将失败。

由于响应者是从Cortex运行的,因此我没有收到“ input:null”以外的错误消息,因此我不得不写入错误日志。使用此日志,我确定错误是由于行data = json.loads(response.text)引起的。我尝试使用简单的json,从response.text中重新指定所需的值,更改编码方法,并从其愚蠢的笨拙的键盘上敲了一下头。

代码:

import requests

import json

from cortexutils.responder import Responder

class Search(Responder):
        def __init__(self):
                # Debug
                with open('/xxx/xxx/xxx/xxx/error.log','a') as error_log:
                        error_log.write('Starting: \n')
                        error_log.write('\n\n')
                # End Debug

                Responder.__init__(self)
                self.apiuser = self.get_param('config.api_user', None)
                self.apikey = self.get_param('config.api_key', None)
                self.url = self.get_param('config.api_url', None)
                self.ioc_type = self.get_param('data.dataType', None)
                self.ioc = self.get_param('data.data', None)
                # Debug
                with open('/xxx/xxx/xxx/xxx/error.log','a') as error_log:
                        error_log.write('User ID: \n')
                        error_log.write(self.apiuser)
                        error_log.write('\n\nSecret: \n')
                        error_log.write(self.apikey)
                        error_log.write('\n\n')
                        error_log.write('IOC Type: \n')
                        error_log.write(self.ioc_type)
                        error_log.write('\n\n')
                        error_log.write('Value: \n')
                        error_log.write(self.ioc)
                        error_log.write('\n\n')
                # End Debug

        def getCount(self):
                with open('/xxx/xxx/xxx/xxx/error.log','a') as error_log:
                        error_log.write('Starting Count: \n')
                        error_log.write('\n\n')

                url = self.url

                headers={'Content-Type': 'application/json'}

                params={'type': self.ioc_type, 'value': self.ioc}

                response = requests.request("GET", url, headers=headers, params = params, auth=(self.apiuser, self.apikey))

                data = json.loads(response.text)
                with open('/xxx/xxx/xxx/xxx/error.log','a') as error_log:
                        error_log.write('Response: ')
                        error_log.write(data)

                deviceCount = data['resources'][0]['device_count']

                self.count = deviceCount



        def run(self):
                Responder.run(self)

                self.getCount()

                self.operations()

        def operations(self):
                return [self.build_operation('AddTagToCase', tag= self.count)]

if __name__ == '__main__':
        Search().run()


Results from response.text:
 {"meta":{"query_time":0.014920091,"trace_id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},
"resources":[{"id":"sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"type":"sha256",
"value":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"device_count":11}],"errors":[]}

Error Logging results:

Starting:


User ID:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Secret:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

IOC Type:
sha256

Value:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

run starting:
Starting Count:

self.count应该等于device_count,但是json.loads无法格式化此响应。在我的错误日志结果中可以看到这一点,其中Count()开始但突然终止,然后才将数据写入其中。

如果您可以深入了解为何无法正确设置响应的格式,请说明一下。

谢谢

1 个答案:

答案 0 :(得分:0)

这是我使用请求模块从API获取json响应的标准方式

response = requests.get(url, headers=headers, params=params, auth=(self.apiuser, self.apikey)).json()
相关问题