Set Post-mortem Executive Summary via API
Asked by Wolfgang Kandek:

I would like to set the Executive Summary in a Post Mortem via the API. I am using Python. There is an API function for it, but unfortunately there is no curl example. Usually with a curl example I can verify that my syntax and formatting works on the commandline and then transfer to Python.  

Does anybody have an example that works?

Here is a snippet of what I am trying to accomplish. The error code that I get when running this is HTTP Error 422: Unprocessable Entity

postmortemid = "0..........8"  
initialURL = "<https://api.opsgenie.com/v2/postmortem/>"  
URL = initialURL + postmortemid + "/executive-summary"  
jsondata = {"type":"adf","executivesummary":{"content":\[{"type":"heading","attrs":{"level":4},"content":\[{"type":"text","text":"Leadup"}\]}\],"type":"doc","version":1}}  
headers = {"Content-Type": "application/json", "Authorization": og_key}  
try:  
data = json.dumps(jsondata)   
bindata = data if type(data) == bytes else data.encode('utf-8')  
req = urllib.request.Request(URL, data=bindata, headers=headers, method='PUT')  
result = urllib.request.urlopen(req)  
if result.getcode() \>= 200 and result.getcode() \< 300:  
messages = result.read()  
json_obj = json.loads(messages)  
print(json_obj)  
else:  
print("Non 200 code from OpsGenie", result.getcode())  
except Exception as e:  
print("Exception")  
print(e)


Answers:
author: Agaci Avinas
body: Hi Wolfgang,

I am Agaci from Opsgenie support. Happy to help you today.

Your code looks good but there are two possibilities for the 422 status.

1. The "S" in the executiveSummary key in the payload should be in upper case as per the [document](https://docs.opsgenie.com/docs/postmortem-api#update-postmortem-executive-summary).

2. 'utf-8' encode is not required as json.dumps itself is going add the escape character to the strings. Adding to that encode the values under executiveSummary inside single quotes.

After the changes your code would like below while constructing and posting the payload,

```
data = {"type":"adf", "executiveSummary":'{"content":[{"type":"heading","attrs":{"level":4},"content":[{"type":"text","text":"Leadup"}]}],"type":"doc","version":1}}'}

jsondata = json.dumps(data) 

req = requests.put(URL, data=jsondata, headers=headers)
```

Regards,

Agaci

comments: author: Wolfgang Kandek
body: Thank you. That helped enormously.  

Here is the final code for future reference (this one is for the body of the post-mortem)  

The main point that tripped me up was that the adf content has to be enclosed by single quotes. In the "data =" line after the first "content" : key note that the entire value is enclosed in single quotes.

postmortemid = '76 .... 6f'  
initialURL = '<https://api.opsgenie.com/v2/postmortem/>'  
URL = initialURL + postmortemid + '/content'

headers = {'Content-Type': 'application/json', 'Authorization': og_key}  
data = {"type":"adf", "content":'{"content":\[{"type":"heading","attrs":{"level":4},"content":\[{"type":"text","text":"Leadup"}\]}\],"type":"doc","version":1}'}

jsondata = json.dumps(data)

req = requests.put(URL, data=jsondata, headers=headers)


Tags: api

cloud

post-mortem