Why does " Task 11 of the API cookbook" raise "HTTPError: 400 Client Error: Bad Request for url"?

Yun Wang
  • 9
  • 24 Apr

Hi,
I'm using the script, given as the Task 11 of the API cookbook, to download some fields for gas, i.e.,

import illustris_request as ilr

base_url = "http://www.tng-project.org/api/Illustris-1/"
sim_metadata=ilr.get(base_url)

for i in range(sim_metadata['num_files_snapshot']):
    file_url = base_url + "files/snapshot-135." + str(i) + ".hdf5"
    saved_filename = ilr.get(file_url, {'gas':'Density,ElectronAbundance,Temperature,GFM_CoolingRate'})
    print(saved_filename)`

in which, illustris_request is

def get(path, params=None):
    # make HTTP GET request to path
    headers = {"api-key": "KEY"}
    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

    if 'content-disposition' in r.headers:
        filename = r.headers['content-disposition'].split("filename=")[1]
        with open(filename, 'wb') as f:
            f.write(r.content)
        return filename # return the filename string


    return r

After running the code, HTTPError: 400 Client Error was raised, that is

File "/Volumes/Seagate/Works/20240822_Work/补充计算20250324/下载文件/download_snapshots.py", line 24, in <module>
    saved_filename = ilr.get(file_url, {'gas':'Density,ElectronAbundance,Temperature,GFM_CoolingRate'})
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Volumes/Seagate/Works/20240822_Work/补充计算20250324/下载文件/illustris_request.py", line 17, in get
    r.raise_for_status()
  File "/Users/wangyun-mac01/opt/anaconda3/lib/python3.11/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.tng-project.org/api/Illustris-1/files/snapshot-135.0.hdf5?gas=Density%2CElectronAbundance%2CTemperature%2CGFM_CoolingRate

Why did this happen?

Dylan Nelson
  • 1
  • 24 Apr

An error is returned because you requested a Temperature dataset for gas, but this does not exist.

You probably mean to put there InternalEnergy, and then calculate temp after.

  • Page 1 of 1