Skip to main content

API-Calls Encoding


Christoph Eibl
Contributor I
Forum|alt.badge.img+1

Hey there,

 

I got a weird problem, which was not reported so far. Or I didn’t find it by now.

I’m sending requests from our Website (Casarista, which works on PHP) to Klaviyo. These calls are made server-to-server in the backend. The problem is, that special characters (which appear in the german language all the time) are not en-/decoded properly.

 

I tried many ways to solve this issue, but nothing worked as expected.

I create my JSON-objects with the json_decode() function of PHP. So far, so well.

An example JSON for this endpoint https://a.klaviyo.com/api/profiles/ is:

 

{"data":{"type":"profile","attributes":{"email":"example@domain.tld","first_name":"Christoph u00e4u00fcu00df 3","last_name":"Eibl"}}}

First name without decoding: “Christoph äüß 3” (for testing purposes I added these special characters)

In Klaviyo the name is displayed as: “Christoph u00e4u00fcu00df 3 Eibl”

 

When I send the special characters without encoding, like this request: 

{"data": {"type": "profile","attributes": {"email": "example@domain.tld","first_name": "Christoph äüß 3ö","last_name": "Eibl"}}}

I get an error from the API itself.

{"errors":[{"id":"ea5ae00e-9055-403a-9172-6670a57561cc","status":400,"code":"parse_error","title":"Malformed request.","detail":"JSON parse error - 'utf-8' codec can't decode byte 0xe4 in position 99: invalid continuation byte","source":{"pointer":"/data/"}}]}

 

I tried:

  • url_encode the values of all strings - didn’t work
  • used several options for the json_decode (JSON_INVALID_UTF8_SUBSTITUTE, JSON_INVALID_UTF8_IGNORE, JSON_UNESCAPED_UNICODE)

 

Is there a way to tell the API endpoint to encode my values?

 

What is wrong with my requests?

 

I’m really desperat and I don’t know what’s wrong with my request. I’m looking forward to your help.

 

Best regards,

Christoph

 

 

p.s. this is the code for sending the requests to the klaviyo API:

<?php
	$curl = curl_init();

	// set all options according to the database values
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $url,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => $curlTimeoutMax,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => $method,
	  CURLOPT_POSTFIELDS => $jsonObject,
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Klaviyo-API-Key ".$klaviyoApiKey,
		"accept: application/json",
		"content-type: application/json",
		"revision: 2023-06-15"
	  ),
	));

	// execute the request
	$response = curl_exec($curl);


?>

 

Did this topic or the replies in the thread help you find an answer to your question?

4 replies

Brian Turcotte
Forum|alt.badge.img+37

Hi @Christoph Eibl!

I’m going to run this by Engineering and I’ll update the thread ASAP!

 

Best,

Brian


Christoph Eibl
Contributor I
Forum|alt.badge.img+1

Hey Brian,

 

did you reach out to your Devs by now?

 

Best regards,

Christoph


Brian Turcotte
Forum|alt.badge.img+37

Hi @Christoph Eibl!

My apologies for the delay here! I spoke with my team and they’re still investigating the cause of this, but I’ll update you as soon as I know more!

 

Best,

Brian


Forum|alt.badge.img+1
  • Klaviyo Employee
  • 10 replies
  • December 6, 2023

@Christoph Eibl Just following up on this, can you confirm whether this is fixed on your end?

 

Thanks,

Jason