code 2001 "title cannot be empty"

rest-api

(Joe Calcada) #1

Hi folks - I’ve been able to successfully create incidents in the developer V2 API reference site but then same json payload running within my python script generates a “2001 error”. I guess HAL is not happy :wink:

can someone see why this code segment would cause that issue ? (my Token is correct and gets applied properly within the developer web site and gets passed into the header correctly in my code below)

import requests
import os
token=os.environ[“PD_API_TEST”]
headerstr = {‘Content-Type’: ‘application/json’, ‘From’: ‘jcalcada@pagerduty.com’, ‘Authorization’: ‘Token token={token}’, ‘Accept’: ‘application/vnd.pagerduty+json;version=2’}
urlstr=“https://api.pagerduty.com/incidents
payloadhead = ‘{\n"incident": { \n “type”: “trigger”,\n’
servicestr =’ “service”: { \n “id”: “P2NMIJW”,\n “type”: “service_reference”\n },\n’
summarystr = ’ “title”: “this is title #1”,\n’
detailstr = ’ “body”: { \n “type”: “incident_body”, \n “details”: “This is detail info” \n }’
payload = payloadhead + summarystr + servicestr + detailstr + ’ \n } \n}’
r = requests.post(urlstr, json=payload, headers=headerstr)
r.raise_for_status
print(f"{payload}")
print(f"{r.json()}")


(Thomas Roach) #2

I’ve had a chat with our tier 2 team, and they’ve suggested that where you’ve written r = requests.post(urlstr, json=payload, headers=headerstr), you should instead use r = requests.post(urlstr, data=payload, headers=headerstr). Give that a try and get back to us with the results!


(Joe Calcada) #3

Yes it works! Thanks.


(Demitri Morgan) #4

FYI, the json keyword argument was meant for dict/list objects; one can pass an object to be automatically JSON-encoded that way versus encoding manually and sending the argument to data.