Form to insert contacts to lists in sendinblue

By: Oskar Hanborg | Asked: 09/09/2022
ForumsCategory: Code HelpForm to insert contacts to lists in sendinblue
Oskar Hanborg asked 2 years ago
Hi community, So what I am trying to do is to make a form using formidable forms to set up a newsletter. The contacts should be sent to sendinblue (the newsletter plugin) that will handle the newsletter. I have followed their offical guide found here: https://formidableforms.com/knowledgebase/formidable-api/how-to-add-contacts-in-sendinblue/ The issue I gets the form is submitted as it seems on the website when testing as a user. However nothing happens other than getting a error log. So no contact are added in sendinblue, and no confirmation email is sent to the user. The error log in formidable forms states this:
{
"headers":
{

},
"body": "
{
"message": "Unexpected token } in JSON at position 226"}", "response":
{
"code":400,"message": "Bad Request"
},
"cookies":[],"filename":null,"http_response":
{
"data":null,"headers":null,"status":null}}
frm_entry   

16
frm_action  

14459
frm_code    

400
frm_message     

Unexpected token } in JSON at position 226
frm_url     

https://api.sendinblue.com/v3/contacts/
frm_request     

{

"updateEnabled": true, "email": "[email protected]", "emailBlacklisted": false, "smsBlacklisted": false,

"attributes": {

"FIRSTNAME": "Oskar", "LASTNAME": "Hanborg",

},

"listIds": [4,5]

}
frm_headers     

Content-type: application/json; charset=UTF-8 api-key: API-key from sendinblue
I looked in my function file on my wordpress server and found that on line 226 (as stated in the log) there is indeed a "}". The problem is I am not a coder, just followed the guide. So to me it seems the code they provide is not working. However they wont give support on code as they told me when I sent in a ticket. Since I have never done coding I tried googleing to understand this but I just learned basic css so this step to troubleshoot code is miles over head. As a total novice/noob I just guessing out load after trying to understand it, and here are my guesses:
  1. Maybe its something with this. But I have no clue how to fix it. I read here: https://bobbyhadz.com/blog/javascript-unexpected-token-u-in-json-at-position-0 It says there that it is a undefined value. If we look at the code in the guide it says
$arg_array[‘headers’][‘api-key’]” But we are only defining the api-key and not the headers – if I understand this (I am a non-coder, just learning by googleing this). Can that be fixed by any chance?

Code that I put in my function file for reference (I change where I need to change):

add_filter( 'frm_api_request_args', 'my_custom_frm_api_request_header', 10, 2 );
function my_custom_frm_api_request_header( $arg_array, $args ) {
    if ( $args['url'] == 'https://api.sendinblue.com/v3/contacts/' ) { // the full url where the request is being sent
        $arg_array['headers']['api-key'] = 'Your-API-Key-Here'; //Replace Your-API-Key-Here with your Sendinblue API key
    }
    return $arg_array;
}
  1. In the log it states “Unexpected token } in JSON at position 226”. If I look in my function file at line 226 then its at that bracket in the code above = the one after/under “your-api-key-here”. Any idea if this could have anything to do with this also?
Many thanks for your time reading this and I am forever grateful if you can help me. Best regards, Oskar I have been writing to support, but they dont give support on code they say. I have been googleing and trying to understand the concept of unexpected token in wordpress, but it goes over my head. I have followed the official guide that should work - so I have no clue what I am doing wrong.
1 Answers
Victor Font answered 2 years ago

You are not creating headers. You are adding the API key to the array element that is used to create the request header further along in the process. If your code is using curly quotes as you are in the example you provided above, it will break the request header. This is your example: $arg_array[‘headers’][‘api-key’], the correct code is: $arg_array['headers']['api-key']. See the difference with the straight quotes? It's corrct in the function block but not when you explain.

You are sending raw JSON as the request. JSON needs to be precise. If you have one misplaced bracket or comma, your code won't work. While the log is helpful, it doesn't show the actual API request or response.

The error message is saying the error is in the form URL. Are you using the correct URL as defined by SendInBlue? APIs often continue to evolve after example code has been written. The example code provided by the Formidable Developers is usually a good starting point, but you have to verify everything with the SendInBlue API documents.

When testing an API, it's a good idea to use Postman until you get a correct response returned from the API. Once the API is working with Postman, then you transfer the raw JSON to Formidable's API add-on and add the field shortcodes. Have you done that?

You have to make sure the data you send matches the the format expected by the sendinblue API. Are you including the country code with the phone number? See this: https://developers.sendinblue.com/reference/createcontact

I would also remove the comments from the raw JSON. Comments aren't always processed by an API and will cause errors. Remove all lines that start with //. Once you have clean JSON with all data elements formatted as the receiving API expects, you should get some kind of response returned from the API.

If you need help from a developer, I suggest you search the Formidable Masterminds Developers Directory. Expect to pay for 4-6 hours of a developers time.

Oskar Hanborg replied 2 years ago

<p>Victor I am almost crying of thankfulness. Your answer made me relook and I found the url from sendinblue documents. It was a / extra in the end in the code I wrote. However now I have a new type of error in the log (so its moving forward). You understood the problem with the first error log (wrong url in the code, was an / extra), can you point me in the right direction on this one also? 🙏 Response { "headers": { }, "body": " { "message": "Key not found", "code": "unauthorized"}n", "response": { "code":401,"message": "Unauthorized" }, "cookies":[],"filename":null,"http_response": { "data":null,"headers":null,"status":null}} frm_entry 23 frm_action 14459 frm_code 401 frm_message Key not found unauthorized frm_url <a href="https://api.sendinblue.com/v3/contacts/&quot; rel="nofollow">https://api.sendinblue.com/v3/contacts/</a&gt; frm_request { "updateEnabled": true, "email": "[email protected]", "emailBlacklisted": false, "smsBlacklisted": false, "attributes": { "FIRSTNAME": "Oskar", "LASTNAME": "Hanborg", }, "listIds": } frm_headers Content-type: application/json; charset=UTF-8</p>

Victor Font replied 2 years ago

This is the error: "Key not found". You're not sending a correct API key. Did you create one in sendinblue? Make sure it's correct.

Oskar Hanborg replied 2 years ago

As I suspected. Yes I made one in sendinblue - copied it to a textfile to save it - then in to the code. I have a ticket at sendinblue regarding this and I included the help you gave here. I dont know whats wrong with the API key provided. I even made a new one today to test a second one - still the same problem 🤷‍♂️

But then hopefully they can arrange a correct API-key and this should be sorted. I will update this thread with what the response from sendinblue 🙂👍

Many thanks for your time and help. It made all the difference 🙏

Oskar Hanborg replied 2 years ago

I read here they changed import of contacts. https://developers.sendinblue.com/changelog/contact-import-endpoint-now-supports-json-body

Could this somehow be linked to what I am trying to do (creating new contacts)?

Oskar Hanborg replied 2 years ago

The problem I had was that the domain was not verified in sendinblue. Also I had not removed the "," in the raw code after removing on attribute I do not use (the sms number). Now it works 🙂👍

Oskar Hanborg replied 2 years ago

To clarify, the code that says ""FIRSTNAME": "Oskar", "LASTNAME": "Hanborg"," should be ""FIRSTNAME": "Oskar", "LASTNAME": "Hanborg"". Its just the end thats different without a ",".

Victor Font replied 2 years ago

That's JSON! JSON format is strict about commas and where they are used. Always run JSON code through JSONLint. It saves heartache.

Oskar Hanborg replied 2 years ago

A funny detail when I look at their website: "Common Errors

Expecting 'STRING' - You probably have an extra comma at the end of your collection. Something like { "a": "b", }" 😅👍

Many thanks Victor 🙏

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right