JSON Input was invalid. Error Message: Syntax error

astegnerbsuastegnerbsu Member Posts: 6

Getting error attempting to POST using Python in VS19. The POST to refresh token works correctly. GETs using the retrieved access token also work. The header works for the GETs. The access token has expired.

r1 = requests.post(endpointCus, headers=_headers, data=payload1).json()

https://api.lightspeedapp.com/API/V3/Account/xxxxxx/Customer.json

{'Content-Type': 'application/json', 'Authorization': 'Bearer bd437db2fc80caf31d6aaafccc946b4de6782eeb'}

I structured the text for the POST to Customer.json with both options, along with using double quote instead of single. The single quotes work in the header. Get same results using the R-seriers Collection in Postman.

{'firstName': 'Adam', 'lastName': 'Stegnerson'}

{'Customer': {'firstName': 'Adam', 'lastName': 'Stegnerson'}}


Always get

{

  "httpCode": "400",

  "httpMessage": "Bad Request",

  "message": "JSON Input was invalid. Error Message: Syntax error",

  "errorClass": "InvalidArgumentException"

}

5 comments

  • Ali_MasoumieAli_Masoumie Moderator, Lightspeed Staff Posts: 422 moderator

    Hi,

    The following should work:

    {
       'Customer': {
          'firstName': 'Adam',
          'lastName': 'Stegnerson'
       }
    }
    
  • astegnerbsuastegnerbsu Member Posts: 6

    Nope, it didn't. Copied it from your message, inserted after '=' when setting the variable

    _headers = {

          'Content-Type': 'application/json',

          'Authorization': 'Bearer ' + _accessToken + ''

          }

    print(endpointCus)

    print(_headers)

    print(" ") 

    payload1 = {

            'Customer': {

             'firstName': 'Adam',

             'lastName': 'Stegnerson'

            }

          }


    r1 = requests.post(endpointCus, headers=_headers, data=payload1).json()

    print(json.dumps(r1, indent=4))

    generates: (with Account number that works with requests.get and fresh access token)


    https://api.lightspeedapp.com/API/V3/Account/xxxxxx/Customer.json

    {'Content-Type': 'application/json', 'Authorization': 'Bearer {Access Token}'}


    {

      "httpCode": "400",

      "httpMessage": "Bad Request",

      "message": "JSON Input was invalid. Error Message: Syntax error",

      "errorClass": "InvalidArgumentException"

    }

    done

    Press any key to continue . . .

  • astegnerbsuastegnerbsu Member Posts: 6

    Any ideas on what I should try next?

  • gregaricangregarican Member Posts: 953 

    Maybe try to capture the API request you are sending out with Fiddler. To see if something is getting hosed up compared to what you think is being sent.

  • astegnerbsuastegnerbsu Member Posts: 6

    Changed

    r1 = requests.post(endpointCus, headers=_headers, data=payload1).json()

    to

    r1 = requests.post(endpointCus, headers=_headers, json=payload1).json()

    and got it to work.

    Thank you for the Fiddler suggestion.

Sign In or Register to comment.