JSON for POST request with non-JSON reply

What I’m trying to do:
I’m trying to send a POST JSON payload to an external API (Garmin InReach Swagger UI). The response is seemingly non-JSON :
HttpErrorStatus: Invalid JSON in response

  • at /libanvil/anvil/http.py:63
  • called from set_track_status, line 40
  • called from Form1, line 34

What I’ve tried and what’s not working:
I’m currently sending a dict using anvil.http.request with the “json=True” tag - that is getting the desired data through to the API, but returning the above error. I obviously need to send my data without the json=True tag, but everything else I’ve tried (JSON dumps, writing a json as a string with triple quotes) throws an error 500.

Can’t figure out how to pass anvil.http.request a JSON payload that doesn’t require json=True in the request line.

Code Sample:

def set_track_status(imei, status,interval):
  
  IMEI = str(imei)
  Interval=str(interval)
  payload_dict = {"Devices": [{"IMEI": IMEI,"Tracking":status,"Interval": Interval}]}
  resp = anvil.http.request(url="https://eur-origin.explore.garmin.com:443/ipcinbound/V1/Tracking.svc/Tracking",
                   method="POST",
                    data=payload_dict, json=True, username="username",password="password")
  return resp

Clone link:
share a copy of your app

Hello John,

Have you tried using requests library? Maybe it can solve your problem.

Link for requests documentation
https://requests.readthedocs.io/en/latest/

Request example in your case:

import requests

def set_track_status(imei, status,interval):
    IMEI = str(imei)
    Interval=str(interval)
    payload_dict = {"Devices": [{"IMEI": IMEI,"Tracking":status,"Interval": Interval}]}

    auth = requests.auth.HTTPBasicAuth('username', 'password')
    response = requests.post(url="https://eur-origin.explore.garmin.com:443/ipcinbound/V1/Tracking.svc/Tracking", 
                             json = payload_dict, 
                             auth=auth)
return response

Top, thanks - didn’t realise requests was one of the supported free tier libraries, but that works fine. Only thing to add is you want to return response.status_code or similar depending on whether you want to use the whole response or just a status code check in your front end code.