James Polo
James Polo

Reputation: 1

DateTime object giving me an error when querying an API using JSON

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.

Upvotes: 0

Views: 63

Answers (1)

James Polo
James Polo

Reputation: 1

I figured it out. Once I figured out it was a GraphQL API, I was able to find code online which slightly changed the requests.post parameters and it worked. You can see below.

headers = {
    'Content-Type': 'application/json',
    'OTPTimeout': '180000',
}
data2 = 'query {trip(from: {coordinates: {latitude:  27.94942 longitude:  -82.47202 }} ,to: \
      {coordinates: {latitude: 27.99107 longitude: -82.45619 }} dateTime: "2024-11-18T16:00:00" ) \
        {tripPatterns {expectedStartTime expectedEndTime duration distance legs{ distance duration id mode }}}}'\
        
           
response = requests.post('http://localhost:8080/otp/transmodel/v3', headers = headers, json = {"query": data2})

json_string = response.text

Upvotes: 0

Related Questions