dannier
dannier

Reputation: 65

How to do this REST API call in Python?

I'm struggling with how to properly do this REST API call in Python using requests package.

curl --user aa43ae0a-a7a7-4016-ae96-e253bb126aa8:166291ff148b2878375a8e54aebb1549 \
  --request POST --header "Content-Type: application/json" \
  --data '{ "startDate": "2016-05-15" , "endDate": "2016-05-16", "format": "tsv", "dataset": "appStart" }' \
  https://analytics.cloud.unity3d.com/api/v2/projects/aa43ae0a-a7a7-4016-ae96-e253bb126aa8/rawdataexports

I don't know, how to set the parameters of:

r = requests.post("https://analytics.cloud.unity3d.com/api/v2/projects/aa43ae0a-a7a7-4016-ae96-e253bb126aa8/rawdataexports", ...)

Any help would be appreciated, thanks in advance.

Upvotes: 1

Views: 389

Answers (1)

Antwane
Antwane

Reputation: 22688

Something like this should do the job :

auth = HTTPBasicAuth('aa43ae0a-a7a7-4016-ae96-e253bb126aa8', '1662[....]549')
payload = {
    "startDate": "2016-05-15" ,
    "endDate": "2016-05-16",
    "format": "tsv",
    "dataset": "appStart",
}
url = "https://analytics.cloud.unity3d.com/api/v2/projects/aa43ae0a-a7a7-4016-ae96-e253bb126aa8/rawdataexports"
result = requests.post(url, json=payload, auth=auth)
  1. Authentication : --user argument of curl comand allows to setup an HTTP Basic Auth
  2. --data was a JSON payload, while the requested content-type is configured (via headers) with Content-Type: application/json. Simplify this with requests using json argument and passing a valid python dict as payload. The payload will be converted to json, and the correct header will be added to the request to retrieve a response with json data.

Be careful ! In your question, you provided the user/password used to call this REST API. If this information is the real one, posting it on Stack Overflow is probably a security issue !

Upvotes: 1

Related Questions