Question

Getting 400 Bad request error while calling the Leads API

Hi, I am working on a product. I made a postman collection for Creatio. Everything works fine inside postman. I am successfully creating leads using the API. Now I am creating a PHP script with the same procedure. After authentication, I am getting the BMPCSRF token in Curl headers. When I try to create a lead, or contact using the updated BMPCSRF token, I am getting "400 bad request" error. I am passing all the required parameters in CURL headers. This is the CURL request.

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://'.  $creatioUrl .'.creatio.com/0/odata/Lead',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>json_encode($data->data),
  CURLOPT_HTTPHEADER => array(
    'ForceUseSession: true',
    'BPMCSRF: '.$arr['BPMCSRF'],
    'Content-Type: application/json',
    'Content-Length: 0'
  ),
));
$response = curl_exec($curl);
curl_close($curl);

Kindly guide me on what I am doing wrong here because nothing is working after the Authentication.

Like 0

Like

4 comments

I assume since this is a POST that this is attempting to add a lead, correct? 

Can you show what the data you're posting looks like? Any Lookup fields would be "LookupNameId" (not just "LookupName"). Also be sure you're including any required fields etc.

Also, maybe try including the Accept header with value "application/json"?

Ryan Farley,

Hi, thanks for the response. Yes, I am using POST to submit a lead. I am sending this data:

{
        "Account": "Awais Khalid",
        "Contact": "Test User",
        "FullJobTitle": "Developer",
        "Email": "testuser1@gmail.com",
        "MobilePhone": "9230068367253",
        "LeadName": "Need for our products / Alexander Wilson, Alpha Business"
    }

I also added the accept header, but getting same error:

400 Bad request - Your browser sent an invalid request.

Here is my PHP code: 

 

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://'.  $creatioUrl .'.creatio.com/0/odata/Lead',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => json_encode($data->data),
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Content-Length: 0',
    'ForceUseSession: true',
    'BPMCSRF: '.$arr['BPMCSRF']
  ),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

I am running this script through postman. This is the payload:

 

I am successfully getting the BPMCSRF token after authentication. But Leads API is not working. I will appreciate your response. Thanks

Awais,

There must be something else wrong with the code. I took your exact same payload and posted it and I can create a lead. I'm just doing it though Postman. 

Headers: 

Body:

Result:

 

I usually bring over the cookies from the auth request as well, not just the BPMCSRF header.

 

Ryan

Hi @Awais, did you ever figure out what was wrong with this code?  We are having a similar issue.

Show all comments