Tuesday, 6 October 2020

[Python] Translating Curl into Python

I needed to Python-ize some code I had written for a Delphix Reporting integration with NetApp’s OCI. This KB for Delphix Reporting uses curl to login to the API and get data:

https://support.delphix.com/Delphix_Reporting/KBA1734_Delphix_Reporting_(Formerly_Mission_Control)_Web_Service_API_Guide

curl --data "password=YOURPASS&user=YOURUSER" http://YOURSERVER/api/login

 

Note: https://curl.trillwords.com is a nice online curl to Python converter (do not enter real data; just use it to get the syntax right.)

 

Translated into Python, the above should be:

 

import requests

 

data = {

  'password': 'YOURPASS',

  'user': 'YOURUSER'

}

 

response = requests.post('https://YOURSERVER/api/login', data=data)

 

Unfortunately, I don’t have success when I try to connect to Delphix Reporting.

 

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

 

I had to put verify=False at the end, like so.

 

import requests

 

data = {

  'password': 'YOURPASS',

  'user': 'YOURUSER

}

 

response = requests.post('https://YOURSERVER/api/login', data=data, verify=False)

 

Great, we have our response but how do we access our required output (loginToken & userId)?

 

import json

json_data = response.json()

loginToken = json_data["loginToken"]

userId = json_data["userId"]

 

Great, so now we know how to get our loginToken and userId. How do we get Storage Summary data?

 

From the KB:

 

curl -H "X-Login-Token: LOGINTOKEN" -H "X-User-Id: USERNAME" "http://YOURSERVER/api/get_report?report=result_storage_summary"

 

This translates to:

 

headers = {

  'X-login-token': loginToken,

  'X-user-id': userId

}

 

params = (

  ('report','result_storage_summary'),

)

 

response = requests.get('https://YOURSERVER/api/get_report', headers=headers, params=params, verify=False)

 

Note: Remember the False in verify=False is case sensitive – it must have a capital F.

 

And this worked. To get the output into JSON format, run:

 

json_data = response.json()

 

And to see the JSON format output, run:

 

json_data


No comments:

Post a comment