简体   繁体   English

Python请求卷曲请求

[英]Python request to curl request

Sample python code which send sample message.发送示例消息的示例 python 代码。

import requests

url = "dns.com/end"
msg = "test connection"
headers = {"Content-type": "application/json",
            "Authorization": "Basic asdfasdf"}

requests.post(url, json=msg, headers=headers)

Now, I'd like to send exactly the same message using curl request.现在,我想使用 curl 请求发送完全相同的消息。

curl -X POST --data "test connection" -H '"Content-type": "application/json", "Authorization": "Basic asdfasdf"' dns.com/end

I'm getting an error: "status":404,"message":"No message available"我收到一个错误:“状态”:404,“消息”:“无可用消息”

You have two issues:你有两个问题:

  • You are not sending JSON data, you forgot to encode your data to JSON.您不是在发送 JSON 数据,而是忘记将数据编码为 JSON。 Encoding the string value test connection to JSON becomes "test connection" , but quotes have meaning in your shell too , so you need to add extra quoting or escapes.将字符串值test connection编码为 JSON 变为"test connection" ,但引号在您的 shell 中也有意义,因此您需要添加额外的引号或转义符。
  • You can't set multiple headers with a single -H entry.您不能使用单个-H条目设置多个标头。 Use multiple, one per header set.使用多个,每个标题集一个。 Headers don't need quotes, only the shell needs quoting to prevent argument splitting on spaces.标题不需要引号,只有 shell 需要引用以防止参数在空格上拆分。

This would be equivalent:这将是等效的:

curl -X POST \
  --data '"test connection"' \
  -H 'Content-type: application/json' \
  -H 'Authorization: Basic asdfasdf' \
  dns.com/end

Demo using https://httpbin.org :使用https://httpbin.org 进行演示:

$ curl -X POST \
>   --data '"test connection"' \
>   -H 'Content-type: application/json' \
>   -H 'Authorization: Basic asdfasdf' \
>   https://httpbin.org/post

{
  "args": {},
  "data": "\"test connection\"",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Authorization": "Basic asdfasdf",
    "Content-Length": "17",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0",
    "X-Amzn-Trace-Id": "Root=1-5e5c399c-201cc8007165873084d4cf38"
  },
  "json": "test connection",
  "origin": "<ip address>",
  "url": "https://httpbin.org/post"
}

which matches the Python equivalent:与 Python 等效项匹配:

>>> import requests
>>> url = 'https://httpbin.org/post'
>>> msg = "test connection"
>>> headers = {"Content-type": "application/json",
...             "Authorization": "Basic asdfasdf"}
>>> response = requests.post(url, json=msg, headers=headers)
>>> print(response.text)
{
  "args": {},
  "data": "\"test connection\"",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Authorization": "Basic asdfasdf",
    "Content-Length": "17",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0",
    "X-Amzn-Trace-Id": "Root=1-5e5c3a25-50c9db19a78512606a42b6ec"
  },
  "json": "test connection",
  "origin": "<ip address>",
  "url": "https://httpbin.org/post"
}

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

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