简体   繁体   English

将此 curl 转换为 Python 请求

[英]Translate this curl to Python request

I was hoping someone can help me translate this curl request into the syntax using Python requests library.我希望有人可以帮助我将这个 curl 请求转换为使用 Python 请求库的语法。 This API is supposed to provide me with a list of tickets.这个 API 应该为我提供一张门票清单。 I am unfamiliar making a request with headers and an API key in Python so I would appreciate the help.我不熟悉在 Python 中使用标头和 API 密钥发出请求,因此我将不胜感激。 Thank you.谢谢你。 I was also getting a 403 Status code when trying to use the base URI.尝试使用基本 URI 时,我也收到了 403 状态代码。

curl request - changed sensative information. curl 请求 - 更改敏感信息。

curl -X POST \
  --url 'https://api.myapi.io/v2/apps/mine/xxx123/data/ticket/search' \
  -H 'x-api-key: myapiKEY123'\
  -H 'Accept: application/json' \
  -H 'Content-type: application/json' \
  --data-raw '{
  "startDate": "2021-03-01",
  "endDate": "2021-04-12"
}'

python code currently目前 python 代码

import json
import requests

API_KEY = "myapiKEY123"
client_ID = 'xxx123'
url = '/v2/apps/mine/' + client_ID + '/data/ticket/search'
params = dict(key=API_KEY, lang='en-es')

requestObject = requests.get(
    url, params=params, headers=headers)
print(requestObject.status_code, requestObject.reason)


Current Error Message:当前错误消息:

requests.exceptions.MissingSchema: Invalid URL '/v2/apps/mine/xxx123/data/ticket/search': No schema supplied. Perhaps you meant http:///v2/apps/mine/xxx123/data/ticket/search?

You don't have a schema (http/https) on your URL, which is also not a complete URL:您的 URL 上没有架构 (http/https),这也不是完整的 URL:

import requests

api_key = "myapiKEY123"
client_id = 'xxx123'
url = f'https://api.myapi.io/v2/apps/mine/{client_id}/data/ticket/search'

headers = {
    'x-api-key': api_key,
    'Accept': 'application/json',
    'Content-type': 'application/json',
}
data = {
    'startDate': '2021-03-01',
    'endDate': '2021-04-12',
}

response = requests.get(url, data=data, headers=headers, timeout=10)

response.raise_for_status()

Then either print the response as JSON (if they're responding with JSON, which is probable) like response.json() or as text response.text .然后将响应打印为 JSON (如果他们使用 JSON 响应,这很可能),例如response.json()或文本response.text The response.raise_for_status() will check if the status code indicated a failure (eg the response code was a 400-599 ) and raise an exception with the status code and the error. response.raise_for_status()将检查状态码是否指示失败(例如, 响应码是 400-599 )并使用状态码和错误引发异常。

You're also not passing the API key in the format the curl is suggesting, which I fixed above.您也没有以 curl 建议的格式传递 API 密钥,这是我在上面修复的。 I also added a timeout, because note that the requests library does not have a timeout by default, so if the server misbehaves the connection will block forever which is not good.我还添加了超时,因为请注意,请求库默认情况下没有超时,所以如果服务器行为不端,连接将永远阻塞,这是不好的。

I also doubt you're supposed to be passing the start and end date as query parameters rather than as data in the body of the request, so I've made that update as well.我也怀疑您是否应该将开始和结束日期作为查询参数而不是作为请求正文中的数据传递,所以我也进行了更新。 Using params would result in a request that looked like this:使用params会产生如下所示的请求:

https://api.myapi.io/v2/apps/mine/{client_ID}/data/ticket/search?startDate=2021-03-01&endDate=2021-04-12

When what the curl (and my above example) is doing is passing those values as JSON in the POST request body.当 curl (以及我上面的示例)正在做的是在 POST 请求正文中将这些值作为 JSON 传递时。

requests.get() is going to return a requests.Response object, so it's also a bit nonsensical to assign it to a variable called requestObject . requests.get()将返回requests.Response object,因此将其分配给名为requestObject的变量也有点荒谬。

Review the following docs:查看以下文档:

It's not a functional problem in your code, but by convention , function and variable names should be lowercase in Python and use underscores to separate terms, not camel case.这不是您的代码中的功能问题,但按照惯例,function 和变量名称在 Python 中应为小写,并使用下划线分隔术语,而不是驼峰式大小写。 Eg response_object rather than responseObject, api_key rather than API_KEY, and client_id rather than client_ID.例如 response_object 而不是 responseObject,api_key 而不是 API_KEY,以及 client_id 而不是 client_ID。 Again, it doesn't make your code not work, it's just convention.同样,它不会使您的代码不起作用,这只是惯例。 You can ignore it, but I wanted to point it out at least.你可以忽略它,但我至少想指出它。

To fix your immediate error, the url you are passing into the requests function is not complete.为了解决您的直接错误,您传递给请求 function 的 url 不完整。 The message you get shows that the url starts with /v2 , while it needs to have the complete url, that is, starting https:// .您收到的消息显示 url 以/v2开头,而它需要完整的 url,即https://开头。

For your header question, the headers is a simple dictionary of key/value pairs, along the lines of:对于您的 header 问题,标题是键/值对的简单字典,大致如下:


headers = {"x-api-key": "myapiKEY123", }

From there, you are passing it into the requests.get function correctly.从那里,您将其传递给requests.get function 正确。

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

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