简体   繁体   English

如何发布 http 请求而不是使用 cURL?

[英]How can I post http request instead of using cURL?

I am using anki-connect to communicate with Anki , a spaced repetition software.我正在使用anki-connectAnki进行通信,这是一个间隔重复的软件。
In readme.md, it uses following command to get deck name.在 readme.md 中,它使用以下命令获取卡组名称。

curl localhost:8765 -X POST -d "{\"action\": \"deckNames\", \"version\": 5}"

It works right in my Windows system.它适用于我的 Windows 系统。
How can I use python instead of cURL?如何使用 python 而不是 cURL?
I've tried this but get no luck.我试过这个,但没有运气。

import requests  
r = requests.post("http://127.0.0.1:8765", data={'action': 'guiAddCards', 'version': 5})
print(r.text)

When creating request you should:创建请求时,您应该:

  • provide Content-Type header提供Content-Type标头
  • provide data in format that matches Content-Type header以与Content-Type标头匹配的格式提供数据
  • make sure application supports the format确保应用程序支持该格式

Both curl and python examples you gave sends request with Content-Type: application/x-www-form-urlencoded , the default one.您提供的curlpython示例都使用Content-Type: application/x-www-form-urlencoded发送请求,这是默认的。 The difference is curl passes string and python passes an array.区别在于curl传递字符串, python传递数组。

Let's compare curl and requests and what is really posted:让我们比较curlrequests以及真正发布的内容:

Curl卷曲

$ curl localhost -X POST -d "{\"action\": \"deckNames\", \"version\": 5}"

Headers:标题:

Host: localhost
User-Agent: curl/7.52.1
Accept: */*
Content-Length: 37
Content-Type: application/x-www-form-urlencoded

Posted data:发布数据:

[
    '{"action": "deckNames", "version": 5}'
]

Python Python

import requests  
r = requests.post("http://127.0.0.1", data={'action': 'guiAddCards', 'version': 5})
print(r.text)

Headers:标题:

Host: 127.0.0.1
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.10.0
Content-Length: 28
Content-Type: application/x-www-form-urlencoded

Posted data:发布数据:

[
    'action' -> 'guiAddCards',
    'version' -> '5',
]

As you can see, incorrect post data format breaks your app.如您所见,不正确的帖子数据格式会破坏您的应用程序。

To be sure, that posted JSON data will be properly read by application you should make requests like that:可以肯定的是,应用程序将正确读取发布的 JSON 数据,您应该发出这样的请求:

Curl卷曲

$ curl localhost:8765 -H 'Content-Type: application/json' -d '{"action": "deckNames", "version": 5}'

Python Python

import requests  
r = requests.post("http://127.0.0.1:8765", json={'action': 'guiAddCards', 'version': 5})
print(r.text)

I've tried following after digging and this works.我在挖掘后尝试过,这有效。
Can anybody share the reason.任何人都可以分享原因。 Thanks.谢谢。

import requests
import json

#r = requests.post("http://127.0.0.1:8765", data={'action': 'guiAddCards', 'version': 5})
r = requests.post('http://localhost:8765', data=json.dumps({'action': 'guiAddCards', 'version': 5}))
print(r.text)

This is a reply to user2444791's answer.这是对 user2444791 的回答的回复。 I can't reply with a comment because I don't have the reputation to comment (I'm new, please forgive a breech of etiquette!)我无法回复评论,因为我没有评论的声誉(我是新人,请原谅礼仪!)

Without the exact error message, it's hard to be sure, but...没有确切的错误消息,很难确定,但是......

Looking at the Anki Connect API , it expects its POST-ed data to be a single string which contains a JSON object, not a key/value dictionary equivalent to that JSON object.查看Anki Connect API ,它期望它的 POST-ed 数据是包含 JSON 对象的单个字符串,而不是等效于该 JSON 对象的键/值字典。

Every request consists of a JSON-encoded object containing an action, a version, and a set of contextual params.每个请求都包含一个 JSON 编码的对象,其中包含一个操作、一个版本和一组上下文参数。

Their example code (in Javascript): xhr.send(JSON.stringify({action, version, params}));他们的示例代码(在 Javascript 中): xhr.send(JSON.stringify({action, version, params}));

It might be as simple as sending your data in the wrong format.这可能就像以错误的格式发送数据一样简单。 In the first example, you are sending a dictionary with the key/vale pairs already parsed.在第一个示例中,您正在发送一个已解析键/值对的字典。 In the second example, you're sending a string for them to parse instead.在第二个示例中,您将发送一个字符串供他们解析。

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

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