[Python] DateTime object giving me an error when querying an API using JSON

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I am using an instance of OpenTripPlanner(route mapping software) and querying its API using JSON. Below is the code:

    headers = {
    'Content-Type': 'application/json',
    'OTPTimeout': '180000',
    }

    data2 = '{"query" :"query {trip(from: {coordinates: {latitude: 27.94942 longitude: -82.47202 }}to:\
    {coordinates: {latitude: 27.99107 longitude: -82.45619 }} dateTime: "2024-11-18T14:00:00.000Z") \
    {tripPatterns {duration distance legs{ distance duration id mode}}}}"}'


    response = requests.post('http://localhost:8080/otp/transmodel/v3', headers = headers, data = data2)

    json_string = response.text


    Error:


    Unexpected character ('2' (code 50)): was expecting comma to separate Object entries at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 167]
    ​

    I have successfully received responses from other queries, but this one, involving a datetime, errors out. I have tried using escaped strings e.g. "2024-11-18T14:00:00.000Z", and also used formatted strings like %s and then adding the value to the end of the string. I don't know if a datetime is a special object, or if I should just be giving it to the api as a string. Any help would be greatly appreciated.

    Continue reading...

Compartilhe esta Página