Getting HTTP POST Error : {"reason":null,"error":"Request JSON object for insert cannot be null."}
Question: I am getting HTTP POST error when I am trying to connect to a Service Now
Instance for Change Request Automation using Python. Here is the script I am
using with 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)
    

I am getting an error as below while running the above script.

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

I am not sure why this error is thrown. Can anybody please help on this ?


Answer: This is a working example I tested on my instance. I am using REST Table API
to insert a change request. It's not true that it can not be http. It's
whatever protocol your instance allows to connect, say from browser.

    
    
    #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)
    

