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

How can I retrieve objects from back4app using python 3?

我正在尝试使用 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': {}}

我真的推荐使用 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())

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

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