Can not save cutout files using python request package

Yan Shulan
  • 3
  • 20 Nov '22

I want to download a cutout file using the method from API documentation. I copy the function :

import requests

baseUrl = 'http://www.tng-project.org/api/'
headers = {"api-key":"1faa547ea961c29000d6c430ce0df54e"}

def get(path, params=None):
    # make HTTP GET request to path
    r = requests.get(path, params=params, headers=headers)

    # raise exception if response code is not HTTP SUCCESS (200)
    r.raise_for_status()

    if r.headers['content-type'] == 'application/json':
        return r.json() # parse json responses automatically
    return r

But when I try to use get function to get and save the hdf5 file (such as saved_filename = get(sub['cutouts']['subhalo'], params)), I can not save the file successfully. I checked the saved_filename, it is not a filename, but just a response. Maybe the reason is my python version (I use python3.7), but I am not sure. So, how can I get and save cutout hdf5 file?

Yan Shulan
  • 20 Nov '22

I have solved this problem. I used wrong "get" function of the Cookbook. I will check my code carefully next time.

Dylan Nelson
  • 20 Nov '22

Yes the get() function is built up, adding more and more functionality, during the tutorial.

But really, you just want to use the "final" version, which is just above the cookbook/reference.

(The version you show above won't download and save files, it will just download and parse JSON responses).

  • Page 1 of 1