djangoeasypost

Django easypost buy returns malformed syntax when using test api key but insufficient funds with production


Using the easypost python library I call the buy function passing in the rate like the documentation says but it returns an error.

Can you use your test api key with buy for easypost or not? I didn't see anything in the documentation with it. It might seem to work with production but I am not able to test that yet so I was wondering if I could test it with the test api key?

The code is:

import easypost

def get_shipment(shipment_id):
    return easypost.Shipment.retrieve(shipment_id)
......
 shipment = get_shipment(shipment_id)
 try:
    shipment.buy(rate=shipment.lowest_rate())
 except Exception as e:
     raise ValidationError({'detail': e.message})

The error message I get with test key

Traceback (most recent call last):
  File "/app/returns/serializers.py", line 237, in handle_shipment_purchase
    shipment.buy(rate=shipment.lowest_rate())
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 725, in buy
    response, api_key = requestor.request('post', url, params)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 260, in request
    response = self.interpret_response(http_body, http_status)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 321, in interpret_response
    self.handle_api_error(http_status, http_body, response)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 383, in handle_api_error
    raise Error(error.get('message', ''), http_status, http_body)
easypost.Error: The request could not be understood by the server due to malformed syntax.


Solution

  • I got the same issue with Python though my shipment id and API_KEY are correct. with the EasyPost python exception message, it will not show the root cause of the exception. Try to do request with curl or inside exception check e.json_body and raise ValidationError accordingly.

     try:
        shipment.buy(rate=shipment.lowest_rate())
     except Exception as e:
        #  Put debugger here and Check exception e.json_body 
        e.json_body
        raise ValidationError({'detail': e.http_body})