获取HTTP POST错误:{“reason”:null,“error”:“请求插入的JSON对象不能为空。”}

时间:2016-10-19 06:31:09

标签: python rest post servicenow

当我尝试使用Python连接到Service Now Instance for Change Request Automation时,我收到HTTP POST错误。这是我在Python 3.4.4中使用的脚本

# SNOW CR AUTOMATION SCRIPT
import requests

import json

# put the ip address or dns of your SNOW API in this url
url = 'http://<>/change_request.do?JSONv2&sysparm_action=insert'


data= {
    'short_description': '<value>',
    'priority': '<value>',
    'reason': '<value>',
    'u_reason_for_change': '<value>',
    'u_business_driver': '<value>',
    'u_plan_of_record_id': '<value>'
      }

print ("Data Inserted :")
print (data)

#Content type must be included in the header
header = {"Authorization":"Basic V1NfRVRPX1ROOkBiY2RlNTQzMjE=","Content-   Type":"application/json"}

#Performs a POST on the specified url.
response = requests.request('POST', url, auth=("<value>","<value>"), json=data, headers=header)

print ( " Header is :  ")
print (response.headers)
print ("                                                                ")
print ( "HTTP Response is :" )
print (response)
print ("                                                                ")
print ("***********************")
print (" Output : ")
print ( response.text)

运行上述脚本时,我收到如下错误。

Output : 
{"reason":null,"error":"Request JSON object for insert cannot be null."}

我不确定为什么会抛出此错误。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我认为你应该使用SSL,所以没有http! 我在脚本中看到的第一个错误是如何传递有效负载,您需要将字典转换为JSON对象/字符串。而且您不需要进行两次身份验证,您可以通过requests.post处理基本的http身份验证,因此在标头中不需要它。

使用此脚本它应该可以工作:

import json
import requests

url = 'https://instancename.service-now.com/change_request.do?JSONv2'
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

payload = {
    'sysparm_action': 'insert',
    'short_description': 'test_jsonv2',
    'priority': '1'
}

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers, data=json.dumps(payload))

# Check for HTTP codes other than 200
if response.status_code != 200:
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

答案 1 :(得分:0)

这是我在我的实例上测试的一个工作示例。我正在使用REST Table API来插入更改请求。这不是真的,它不能是http。它是您的实例允许连接的任何协议,例如来自浏览器。

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = '<yourinstance base url>/api/now/table/change_request'
user = <username>
pwd = <password>

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\"short_description\":\"test in python\"}")

# Check for HTTP codes other than 201
if response.status_code != 201: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)