-2

Below is the api response im getting :::

{"contentType":"image/jpeg","createdTime":"2021-10-10T11:00:47.000Z","fileName":"Passport_Chris J Passport Color - pp.jpg","id":10144,"size":105499,"updatedTime":"2021-10-10T11:00:47.000Z","links":[{"rel":"self","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/CompanyRegd.ManagerDetails/43/FileAttachments/10144?download="},{"rel":"canonical","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/CompanyRegd.ManagerDetails/43/FileAttachments/10144"},{"rel":"describedby","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/metadata-catalog/CompanyRegd.ManagerDetails/FileAttachments","mediaType":"application/schema+json"}]}

i need to save this file as jpg format locally to my system? could you please provide me a solution through python

Jens
  • 67,715
  • 15
  • 98
  • 113

1 Answers1

0

You might have to decode the JSON-string (if not already done):

import json
json_decoded = json.loads(json_string)

Afterwards you can get the URL to retrieve and the filename from this JSON-structure

url = json_decoded['links'][0]['href']
local_filename = json_decoded['fileName']

Now you can download the file and save it (as seen here How to save an image locally using Python whose URL address I already know?):

import urllib.request
urllib.request.urlretrieve(url, local_filename)
Matthias Radde
  • 161
  • 1
  • 3
  • 8
  • I am getting an error at the last line where we save locally, it shows HTTP Error 400: Bad Request File "C:\Users\Alzone.DESKTOP-BINFAND\Documents\Uiauto\tesst.py", line 6, in urllib.request.urlretrieve(url, local_filename) – muhamed fasil Sep 07 '22 at 08:33
  • @muhamedfasil If I request the mentioned URL I get an errormessage within the server's response "Request does not contain OSvC-CREST-Application-Context header". Something is missing in the request - maybe some credentials? – Matthias Radde Sep 07 '22 at 09:38