如何使用python 3从back4app中检索对象?

时间:2018-03-30 15:25:50

标签: python python-3.x back4app

我正在尝试使用python与存储在back4app中的数据库进行交互。发送GET请求后,我收到“{'message':'Not Found','error':{}}”。我的python代码如下:

import json, http.client, urllib.parse
# create a connection to the server
url = "parseapi.back4app.com"
connection = http.client.HTTPSConnection(url)
connection.connect()

# define parameter for GET request
params = urllib.parse.urlencode({"where":json.dumps({"Name": "Dru Love"})})

# perform GET request
connection.request('GET', '/parse/classes/PGA?%s' % params, '', {
       "X-Parse-Application-Id": "app_id",
       "X-Parse-REST-API-Key": "api_key"
     })

# store response in result variable
result = json.loads(connection.getresponse().read())
connection.close()
print(result)

响应:

{'message': 'Not Found', 'error': {}}

2 个答案:

答案 0 :(得分:0)

我真的建议使用requests包。该套餐非常适合HTTP次来电。

你案子的一个小例子。

# Your payloads
params = {
    "X-Parse-Application-Id": "app-id",
    "X-Parse-REST-API-Key": "YOUR_IP_KEY"
}


url = 'https://parseapi.back4app.com/parse/classes/Congressmen'


# GET method, with URI parameters
response = requests.get(url, params=params)

print(response.url)

# Get json data from a response.
print(response.json())

答案 1 :(得分:0)

问题在于对connection.request()的调用。应该从传入的url中删除“/ parse”。工作代码如下所示:

connection.request('GET', '/classes/PGA?%s' % params, '', {
       "X-Parse-Application-Id": app_id,
       "X-Parse-REST-API-Key": api_key,
   })