errors in PHP calls to Insightly API

Getting an error when I try to post a form through AJAX to PHP and create a new Contact with the Insightly API. I think the error has to do with the way the form data gets encoded and added to the JSON object, but I've been unable to tell specifically what characters are causing the error. There doesn't seem to be a pattern--sometimes it breaks when you enter one word, no special characters, other times you can post in all sorts of commas, apostrophes etc with no issue.

The PHP that posts to Insightly is here:

(I did not write this; I'm taking over for the original developer)

The error log:

Message: Exception: Bad HTTP status code: 400

InsightlyRequest::asString called at [/app/assets/php/insightly.php:1539]

InsightlyRequest::asJSON called at [/app/assets/php/insightly.php:236]

Insightly::addContact called at [/app/assets/php/insightly_send.php:126]

Javascript is: 

  method: "POST",
  url: "/assets/php/insightly_send.php", 
  data: {
    type: "contact",
    name: name,
    email: email,
    location: location,
    comment: comment,
    organisation_id: organisation_id

where all the post variables are form field data. 

I know it has to do with that comment field but can't determine how or if to sanitize the form input so this doesn't happen.

This is more of a general PHP question rather than specific to Insightly but hoping someone has had a similar issue or can provide insight (heh)



  • Official comment


    From what I can tell the request looks valid to me. You might want to try making the request to the version 2.2 API ({endpoint})

    The new version provides better error messages so you will have a better idea of what is causing the bad request error (this is usually due to a missing required field, or bad data).


    Brian McConnell

    Insightly Engineering Team

    Comment actions Permalink
  • Thanks for responding, Brian. 

    After hours of frustration, I determined the errors were only coming from users who had entered line breaks in the form's comment field (the data-->comment part of the post). The previous developer hadn't been calling json_encode on the form data before building the JSON object that gets sent to Insightly, so these line breaks weren't getting encoded as \n and causing a JSON error. 

    I will upgrade to 2.2 though, because some better error messages would have been nice! Haha.



    Comment actions Permalink

Please sign in to leave a comment.