如何使用Requests模块在Python 3中发布此API代码?继续得到405错误

Jus*_*kie 2 python token python-requests

我想将API用于链接缩短程序站点.我知道给出的例子是curl,这-H意味着它有一个标题以及PUT它的发布意味着,但无论我多少尝试,我似乎无法得到我想要的结果.我所能做的就是得到405错误或打破它.

我在Windows上使用Python 3并且已经requests安装了.

对于开发人员Shorte.st准备的API,它以JSON格式返回响应.

目前,有一种方法可用于代表您的帐户缩短链接.

请查看下面的示例,了解如何使用我们的API.

curl -H "public-api-token: (my token)" -X PUT -d
"urlToShorten=google.com" https://api.shorte.st/v1/data/url 
Run Code Online (Sandbox Code Playgroud)

收到时,

{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
Run Code Online (Sandbox Code Playgroud)

我尝试了一些东西,

import requests
import json
gogo = { 'public-api-token: (my token)' : 'urlToShorten=google.com'}
yep = requests.post('https://api.shorte.st/v1/data/url',   
data=json.dumps(gogo))
print (yep.text)
Run Code Online (Sandbox Code Playgroud)

向我显示错误405的HTML网页.

import requests
import json
gogo = { 'public-api-token' : '(my token)', 'urlToShorten=' : 'google.com'}
yep = requests.post('https://api.shorte.st/v1/data/url',    
data=json.dumps(gogo))
print (yep.text)
Run Code Online (Sandbox Code Playgroud)

还向我显示了错误405的网页.

我现在知道它-H是用于标题而我正在使用它,同时仍然只是让我的页面.

import requests
import json

headers = { 'public-api-token' : '(my token)' }
gogo = {"urlToShorten" : "google.com"}
yep = requests.post('https://api.shorte.st/v1/data/url',  
data=json.dumps(gogo), headers=headers)
print (yep.text)
Run Code Online (Sandbox Code Playgroud)

另一次尝试另一次405

gogo = {"public-api-token: (my token)" : "urlToShorten=google.com"}
yep = requests.post('https://api.shorte.st/v1/data/url', 
data=json.dumps(gogo))
print (yep.text)
Run Code Online (Sandbox Code Playgroud)

如果我取消文本,即使这只是给我一个完整的HTML页面/ 405.

headers = { "public-api-token: (my token)" : "urlToShorten=google.com" }
yep = requests.post('https://api.shorte.st/v1/data/url', headers=headers)
print (yep.text)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

您将PUT有效负载放在标头中.把它放在身体里.您的有效负载不是 JSON,因此无需尝试对其进行处理.

标题需要指定为字典,标题名称和标记为键和值.可以以相同的方式处理有效载荷的参数.

你也使用了错误的requests方法; 要发送PUT请求,请使用该put功能:

headers = {'public-api-token': '(my token)'}
payload = {'urlToShorten': 'google.com'}
response = requests.put(
    'https://api.shorte.st/v1/data/url',
    data=payload, headers=headers)

print(response.json())
Run Code Online (Sandbox Code Playgroud)

response对象有一个json()方法来解码API返回的JSON数据; 它将为您提供与JSON文本相对应的Python数据结构.

我没有您正在使用的服务的令牌; 我使用该httpbin.org服务创建了一个演示; 它反映了作为JSON响应发送的内容:

>>> import requests
>>> headers = {'public-api-token': '(my token)'}
>>> payload = {'urlToShorten': 'google.com'}
>>> response = requests.put(
...     'http://httpbin.org/put',
...         data=payload, headers=headers)
>>> from pprint import pprint
>>> pprint(response.json())
{u'args': {},
 u'data': u'',
 u'files': {},
 u'form': {u'urlToShorten': u'google.com'},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate',
              u'Content-Length': u'23',
              u'Content-Type': u'application/x-www-form-urlencoded',
              u'Host': u'httpbin.org',
              u'Public-Api-Token': u'(my token)',
              u'User-Agent': u'python-requests/2.5.0 CPython/2.7.9 Darwin/14.1.0'},
 u'json': None,
 u'origin': u'94.118.96.0',
 u'url': u'http://httpbin.org/put'}
Run Code Online (Sandbox Code Playgroud)

如果将其与为curlPUT同一URL 发送请求而生成的输出进行比较,您将看到生成相同的结果:

$ curl -H "public-api-token: (my token)" -X PUT \
     -d "urlToShorten=google.com" http://httpbin.org/put
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "urlToShorten": "google.com"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "Public-Api-Token": "(my token)", 
    "User-Agent": "curl/7.37.1"
  }, 
  "json": null, 
  "origin": "94.118.96.0", 
  "url": "http://httpbin.org/put"
}
Run Code Online (Sandbox Code Playgroud)