用于发布api的Python脚本

时间:2017-03-15 06:19:46

标签: python-2.7 rest

我正在使用以下脚本将API发布到我的应用程序

#!/usr/bin/python

import sys
import getopt
import smtplib
import requests
import json

payload = {
"eventType": "xxxx-cnsn-32324-333-22323",
"shortDesc" : "Sample Event",
"systemIdentity" : {
"ip" : "127.0.0.1"
},
"user" : "test user",
"properties" : {
  "DeviceName":"DeviceName",
  "AlertType":"NodeDown",
  "occurrenceTime":"1033432123432"
  }

r = requests.post('http://www.testapi.com/api/v3/events', payload)
print(r.url)

我收到以下错误

File "./test.py", line 22
r = requests.post('http://www.testapi.com/api/v3/events', payload)
^
SyntaxError: invalid syntax

有什么想法吗?我正在使用Python 2.7

1 个答案:

答案 0 :(得分:-1)

首先,您忘记添加data关键字

r = requests.post('http://www.testapi.com/api/v3/events', data = payload)

有效载荷的括号是错误的。

payload = {
"eventType": "xxxx-cnsn-32324-333-22323",
"shortDesc" : "Sample Event",
"systemIdentity" : {
"ip" : "127.0.0.1"
},
"user" : "test user",
"properties" : {
  "DeviceName":"DeviceName",
  "AlertType":"NodeDown",
  "occurrenceTime":"1033432123432"
  },
}