简体   繁体   English

Python的请求和流星/ MongoDB的collectionapi更新不工作

[英]Python-requests and Meteor / MongoDB collectionapi update not working

I'm trying to use Python Requests to send data to a Meteor application. 我正在尝试使用Python请求将数据发送到Meteor应用程序。 I'm using the meteor-collectionapi to expose my collection. 我正在使用meteor-collectionapi公开我的收藏。

I can use CURL to update my collection, like so: 我可以使用CURL更新收藏集,如下所示:

curl -H "X-Auth-Token: 3243EEREFADfdsafkjghk432hljsfDS3" -X PUT -d "{\"\$set\":{\"level\":\"32\"}}" http://localhost:3000/collectionapi/containers/WjyuFkRdmq78qyzR7`

I'd like to perform the same command in Python using Requests. 我想在Python中使用Requests执行相同的命令。 Here's the code I've put together: 这是我编写的代码:

import requests
import json
url = 'http://localhost:3000/collectionapi/containers/WjyuFkRdmq78qyzR7'
headers = {'X-Auth-Token': '3243EEREFADfdsafkjghk432hljsfDS3'}
payload = {'\$set':{'level':'43'}}
r = requests.post(url, data=json.dumps(payload), headers=headers)

When I run this the $set doesn't get passed properly and the POST doesn't work correctly (it creates a new object in the collection instead of updating the existing object). 当我运行这个$set不得到正确过去了,POST不能正常工作(它创建集合在一个新的对象,而不是更新现有对象)。 I've tried escaping it a variety of ways but nothing seems to work properly. 我尝试了多种方式进行转义,但似乎没有任何正常工作。 If I don't escape the $set I get: 如果我不逃避$set我得到:

payload = {'$set':{'level':'38'}} 
{"error":"Error: key $set must not start with '$'"}

In your cURL command you're not doing a POST, you're doing a PUT. 在cURL命令中,您未执行POST,而是在执行PUT。

Try changing your requests command to 试着改变你的请求命令

r = requests.put(url, data=json.dumps(payload), headers=headers)

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

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