1

I want to build a script that run daily, to retrieve (yesterday date - today date) data.

A code to get today, yesterday date in ISO 8601, since the application only accept ISO 8601 format.

currDate = datetime.datetime.now()
prevDate = datetime.datetime.today() - datetime.timedelta(days=1)
currDateISO = currDate.isoformat()
prevDateISO = prevDate.isoformat()

Post above ISO date into Request body.

DATA = \
    {'verb': 'query', \
    'start_time': prevDateISO, \
    'end_time': currDateISO \
    }

res=requests.post(Host + url, headers=Header, data=json.dumps(DATA), verify=False)

Error (Python):

 'error': 'invalid parameter', 'message': 'Error in field start_time: Time format must be ISO8601'}

Taking out the start/end time, worked in my Python codes.

I was able to Post using below syntax:

{
  "verb":"query",
  "start_time":"2019-12-17T15:39:55.616Z",
  "end_time":"2019-12-18T15:39:55.616Z"
}
  • I can't get what the issue actually is. Can you provide more details? – Henry Harutyunyan Dec 19 '19 at 09:29
  • If you are getting the error in the API request why don't you try to put the date manually (as a string) then test the request? If you don't get an error see what's the difference between Python's iso date and the one you need. – Henry Harutyunyan Dec 19 '19 at 09:31
  • Thanks @HenryHarutyunyan. I want to retrieve a data from this Symantecc ATP (API) . The request body (start-end time) only accept date in ISO format. I want to to tell the script, to get today & tomorrow date & post the request. Meaning, "start_time" will get yesterday's date. Similar like query condition; Yesterday - Now. Sorry, I'm not really experts in programming terms. – malFUNKtion Dec 19 '19 at 09:40
  • np. But I guess you are receiving an error from the API. Can you try setting the dates manually? I mean just use strings of some random dates and see what the response is. – Henry Harutyunyan Dec 19 '19 at 09:43
  • @HenryHarutyunyan, I mention that in my question. Using full ISO date is working. – malFUNKtion Dec 19 '19 at 09:44
  • Ok then you can use `strftime` to convert the date to your desired format in stead of the python's `isodate` method. – Henry Harutyunyan Dec 19 '19 at 09:46
  • see [this](https://stackoverflow.com/questions/2158347/how-do-i-turn-a-python-datetime-into-a-string-with-readable-format-date) – Henry Harutyunyan Dec 19 '19 at 09:47

1 Answers1

1
currDateISO = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
prevDateISO = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")

ISO time (ISO 8601) in Python