Solved

PHP SDK 400 bad request

  • 21 December 2022
  • 13 replies
  • 947 views

Badge +1

Hi all,

Just started using the Klaviyo SDK and after several hours and reading api guides and postman quiries i cannot figure this out.

This is my error below

[400] Client error: `POST https://a.klaviyo.com/api/profiles/` resulted in a `400 Bad Request` response: {"errors":[{"id":"edd7f096-d88a-4b8c-80cd-175250722bfe","status":400,"code":"invalid","title":"Invalid input.","detail": (truncated...)

 

So FYI i can make get request like this and everything works great:

$klaviyo->Lists->getList('******');

 

As soon as i want to make a post request i try to send an associative array over the the keys of data , type and attributtes and no matter what way i send this data i even tried json etc i get the above error could anyone help?

$array[] = ['data' => ['type' => 'profile', 'attributes' => ['email' => 'test@outlook.com', 'first_name' => 'elliot',     'title' => 'fddfasdsfaf', 'organization' => 'fsdfdsfsd',    'external_id' => '1234', 'anonymous_id' => '12345', 'image' => 'dfds']]];
$response = $klaviyo->Profiles->createProfile($array);

 

 

icon

Best answer by Brian Turcotte 22 December 2022, 00:24

View original

13 replies

Userlevel 7
Badge +36

Hi @ellputt and welcome to the Community!

 

Typically, 400 responses occur when the request is missing or the API key is invalid. Would you be willing to share the entire non-truncated error statement? I’m going to reach out to Engineering to gather some further context on this issue, so it will help to understand the issue better.

 

 

Thanks,

Brian,

Badge +1

Hi thanks for the response I have since used the direct api instead of the sdk all I get is the truncated error unfortunately no further message. I have fixed now thank you ! 

Badge

I also have this frustrating issue. 
I docs there is clear format of JSON data then needs to be sent: https://developers.klaviyo.com/en/v1-2/docs/guide-to-integrating-a-platform-without-a-pre-built-klaviyo-integration#placed-order
 

But as soon as I reformat this data to PHP array format so I can use your PHP SDK package it returns error like this: 

'[400] Client error: `POST https://a.klaviyo.com/api/events/` resulted in a `400 Bad Request` response:

{"errors":[{"id":"22a89f30-3e98-4575-802b-572151897937","status":400,"code":"invalid","title":"Invalid input.","detail": (truncated...)

 

Please be advised this is not issue with API keys. This is error because of some field cannot be inserted.
I cannot know which since error msg is truncated! Please, please provide entire error message to end user, this is really poor decision to truncate error from user. I am now trying like chicken in a fog to find a way how can this be done.

Also in your PHP SDK docs, there should be clear indication and example or POST data that can be sent. It really, really simple for your DEV to add this examples of arrays to docs.
Without this thousands of DEV will lose thousands of hours researching this and nagging your support.

Anyway, can I have this secret data PHP array example data for placed order event? 
Just copy example array of data that needs to be posted and I will be on my way…
Thanks!


 

Userlevel 7
Badge +36

Hi @aleksandar.mitic!

 

I will definitely forward this feedback to the Product team, as I agree that more robust error messages would be a valuable upgrade. As far as the PHP array example, I will reach out to Engineering and then update the thread, and I’ll also see if that can be added to the docs. 

 

Thanks for using the Community!

- Brian

The issue still persists. I am using new PHP API SDK and I am having trouble with POST subscribeProfiles API. If I use Postman, with JSON body (which is decoded from the PHP body), I get 202 SUCCESS status. If I try the same data as an associative array in PHP, I get 400 status and the details parameter has “(truncated...)” value. Please help as this is a crucial piece of functionality my company is working on.

Userlevel 7
Badge +36

Hi @Plutarch87!

My apologies for the delay here - I think the best route would be to create a ticket with Support for this issue as they will have higher-level access to best troubleshoot the formatting error.

 

Best,

Brian

Badge +2

Hi @Plutarch87 Did you solve this issue? I have the same issue using the SDK for PHP
This is the error I got 
 KlaviyoAPI\ApiException  [400] Client error: `POST https://a.klaviyo.com/api/profiles/` resulted in a `400 Bad Request` response:
{"errors":[{"id":"a9080d30-8e7c-446e-b384-...","status":400,"code":"invalid","title":"Invalid input.","detail": (truncated...).

Hi @Plutarch87 Did you solve this issue? I have the same issue using the SDK for PHP
This is the error I got 
 KlaviyoAPI\ApiException  [400] Client error: `POST https://a.klaviyo.com/api/profiles/` resulted in a `400 Bad Request` response:
{"errors":[{"id":"a9080d30-8e7c-446e-b384-...","status":400,"code":"invalid","title":"Invalid input.","detail": (truncated...).

I didn’t unfortunately. I had to use guzzleHttp client raw post method

Badge +2

@Plutarch87 sorry for bothering you again but as you know the documentation lacks of information and I’m clueless how to implement the SDK.
I created a KlaviyoServiceProvider 

public function register()
    {
        $this->app->singleton(Klaviyo::class, function() {
            return new Klaviyo(new KlaviyoAPI($this->config['private_api_key']));
        });
    }

and then a Klaviyo service
 

 public function createKlaviyoProfile(User $user)
    {
        $body = array(
            "data" => array(
                "type" => "profile",
                "attributes" => array(
                    "email" => $user->email,
                    "phone_number" => $user->shipping_phone_number,
                    "external_id" => $user->id,
                    "first_name" => $user->firstName(),
                    "last_name" => $user->lastName(),
                    "organization" => null,
                    "title" => null,
                    "image" => null,
                    "location" => array(
                        "address1" => $user->shipping_address_line1,
                        "address2"=> $user->shipping_address_line2 ?? null,
                        "city" => $user->shipping_address_city,
                        "country" => "United States",
                        "region" => $user->shipping_address_state,
                        "zip" => $user->shipping_address_postal_code,
                        "timezone" => "America/New_York",
                    ),
                    "properties" => array(
                        "newKey" => "New Value",
                    ),
                ),
            ),
        );

        $klaviyo = new KlaviyoClient(
            $api_key = '$this->config['private_api_key']',
        );

        try {
            $klaviyo->Profiles->createProfile(json_encode($body));
        } catch (Exception $exception) {
            echo $exception;
        }
    }

Do you think this is a good approach?

No problem. That approach didn’t work for me. I had to manually call the client
 

$client = new \GuzzleHttp\Client();

$response = $client->request(‘POST’, 'https://a.klaviyo.com/api/profile-subscription-bulk-create-jobs/',
[
    ‘body’ => json_encode($body), /* body can be encoded php array
    ‘headers’ => $this->headers /* found in the Klaviyo docs */
]);

Hope this helps

Badge +2

Thank you @Plutarch87 I think I will switch to call it manually, is a shame the SDK does not work as suppose to!

Badge +2

Hi @Plutarch87 after being working a lot with the PHP SDK I found out why I was getting the error.
I don’t know if you did the same in your code but just in case here is what i found out.

I was doing this `$this->klaviyoClient->Profiles->createProfile(json_encode($body));` I removed the `json_encode` and I passed just the `$body` (an associative array) and now it works!!

I tested with :
1. Profiles->subscribeProfiles($body);
2. Profiles->createProfile($body);

I had the same issue as couldn’t figure out the format of the PHP associative array to pass (no examples seem to be given anywhere!).  

This is what I used to get it to work - hope it helps somebody!

 

 $body =['data' => ['type' => 'profile', 'attributes' => ['email' => 'test@outlook.com', 'first_name' => 'elliot',     'title' => 'fddfasdsfaf', 'organization' => 'fsdfdsfsd',    'external_id' => '1234', 'anonymous_id' => '12345', 'image' => 'dfds']]];

$klaviyo->Profiles->createProfile($body);

 

 

 

Reply