使用python发出POST请求

时间:2016-03-18 14:54:54

标签: python post python-requests

我已经购买了一个接收GET请求的API,例如:/myapi.com/login?username=me&password=mypass,并为我提供了一个唯一的令牌。使用该标记,我可以通过API发送电子邮件。问题是我已经认证,我收到我的令牌,我把它放在标题中以便发送邮件请求我无法发送电子邮件:服务器说:“500内部错误完全验证需要访问此资源”

有我的python脚本:

#! /usr/bin/python2.7

import requests
import json
import unirest

# Connection  
payload = {'username':'monusername', 'password':'monpassword'}
r = requests.get('monapi.com/login', params=payload)
print(r.url, r.content, r.json, r.status_code)
# Recup the token
token = json.loads(r.text)
token = str(token['token'])
# we want to send an email
payload2 = {'from': 'v.test@outlook.com', 'to': 'v.test@outlook.com', 'subject': 'test de mon script', 'content': 'salutations vous etes le premier a recevoir un de mes emails'}
headers = {'Content-Type':'application/json', 'token': token}
r2 = requests.post('monapi.com/front/fSend', headers=headers, data=json.dumps(payload2))

print(r2.status_code,r2.request.headers, r2.request.body)

服务器响应是:

(u'monapi.com/login?username=monusername&password=monpassword', '{"name":"monusername","password":"86057438fbc6889b91f258c30d1da4db8d71d7c8","token":"a89ba0ac6885668e2d7d983c36af845e","id":1}', <bound method Response.json of <Response [200]>>, 200)
(500, {'Content-Length': '180', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Accept': '*/*', 'User-Agent': 'python-requests/2.9.1', 'token': 'a89ba0ac6885668e2d7d983c36af845e', 'Content-Type': 'application/json'}, '{"content": "salutations vous etes le premier a recevoir un de mes emails", "to": "v.tests@outlook.com", "from": "v.tests@outlook.com", "subject": "test de mon script"}').

一切都可以使用CORS扩展RESTClient但不能在我的服务器上运行....

0 个答案:

没有答案