JSONObject文本必须以字符1处的“{”开头

时间:2016-04-08 14:02:47

标签: python json python-requests

这是我的第一篇文章,请原谅任何错误。 问题: 我在运行代码时收到以下错误。

JSONObject文本必须以config = type

的字符1处的“{”开头

阅读了很多帖子之后,这似乎是一个常见的问题,但我还没有找到解决方案。

我的代码(删除了身份验证详情):

import base64

import requests
from requests import Request, Session

import re
import json

# Developing URL strings with session login
auth_string = user + ':' + password  # preps login
credentials = base64.b64encode(auth_string)  # base 64 is the preferred format for http communication
url_base = 'https://' + esm + '/rs/esm'  # this is the url to send commands to
login_url = url_base + '/login'  # logging in to system

# header for session persistence
login_headers = {'Authorization': 'Basic ' + credentials, 'Content-Type': 'application/json'}
login_response = requests.post(login_url, headers=login_headers, verify=False)
session = login_response.headers['location']
session_header = {'Authorization': 'Session ' + session, 'Content-Type': 'application/json'}

# commands to get reports
version = requests.get(url_base + '/getVersion', headers=session_header, verify=False)
dataReturned = requests.post(url_base + '/devGetDeviceList?filterByRights=false', data={"type": "RECEIVER"},
                             headers=session_header, verify=False)

# print results
print(version.content)
print(version.url)
print(dataReturned.url)
print(dataReturned.content)

# terminate session
requests.delete(url_base + '/logout', headers=session_header, verify=False)

以下是运行代码的输出:

C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py:789: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py:789: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py:789: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
{"return": {
    "apiMajorVersion": 3,
    "apiMinorVersion": 0,
    "esmMajorVersion": 9,
    "esmMinorVersion": 5,
    "esmPatch": "",
    "esmRevision": 2,
    "esmVersionString": "9.5.2 20160128"
}}
https://IP/rs/esm/getVersion
https://IP/rs/esm/devGetDeviceList?filterByRights=false
A JSONObject text must begin with '{' at character 1 of type=RECEIVER
C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py:789: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)

Process finished with exit code 0

2 个答案:

答案 0 :(得分:0)

看起来您正在与之通信的Web服务需要JSON,而您却没有提供它。当你跑:

dataReturned = requests.post(url_base + '/devGetDeviceList?filterByRights=false',
  data={"type": "RECEIVER"},
  headers=session_header, verify=False)

您正在发送包含内容的application/x-www-form-urlencoded正文:

type=RECEIVER

如果要将JSON发送到远程服务器,则需要调用json.dumps

dataReturned = requests.post(url_base + '/devGetDeviceList?filterByRights=false',
  data=json.dumps({"type": "RECEIVER"}),
  headers=session_header, verify=False)

http://requestb.in这样的测试服务可以非常有助于验证您是否正在发送您认为正在发送的请求。

答案 1 :(得分:0)

就像我之前在评论中写的那样,我怀疑你是在传入一个预期有json对象的字典。因此错误:

  

JSONObject文本必须以类型= RECEIVER

的字符1处的“{”开头

这是分配给变量data的字典:

data={"type": "RECEIVER"}

这两个是json。尝试其中一个:

data='{"type": "RECEIVER"}'
data=json.dumps({"type": "RECEIVER"})