I’m currently working with an API and Elementor, and I’m trying to send form information to the platform API. However, I’m encountering a 400 Bad Request error when making a request to my server. The error message indicates that some parameters are missing from my request. Here’s the error from debug_log:
{
"code": "400",
"error": "Bad Request",
"msg": "Some parameters are required",
"schema": [
{
"customer_name": "my name",
"customer_last_name": "my last name",
"id_type": "CC",
"customer_id": "113xxxx00",
.....
}
]
}
This is the error from admin-ajax.php :
array(4) { ["code"]=> string(3) "400" ["error"]=> string(11) "Bad Request" ["msg"]=> string(28) "Some parameters are required" ["schema"]=> array(1) { [0]=> array(37) { ["customer_name"]=> string(7) "my name" ["customer_last_name"]=> string(12) "my last name" ["id_type"]=> string(2) "CC" ["customer_id"]=> string(9) "113xxxx00" ["age"]=> string(2) "30" ["gender"]=> string(1) "M" ["country"]=> string(3) "COL" ["state"]=> string(0) "" ["city"]=> string(0) "" ["zone"]=> string(0) "" ["address"]=> string(0) "" ["opt1"]=> string(0) "" ["opt2"]=> string(0) "" ["opt3"]=> string(0) "" ["opt4"]=> string(0) "" ["opt5"]=> string(0) "" ["opt6"]=> string(0) "" ["opt7"]=> string(0) "" ["opt8"]=> string(0) "" ["opt9"]=> string(0) "" ["opt10"]=> string(0) "" ["opt11"]=> string(0) "" ["opt12"]=> string(0) "" ["tel1"]=> string(11) "9301500xxxx" ["tel2"]=> string(0) "" ["tel3"]=> string(0) "" ["tel4"]=> string(0) "" ["tel5"]=> string(0) "" ["tel6"]=> string(0) "" ["tel7"]=> string(0) "" ["tel8"]=> string(0) "" ["tel9"]=> string(0) "" ["tel10"]=> string(0) "" ["tel_extra"]=> string(0) "" ["email"]=> string(0) "" ["recall_date"]=> string(14) "YYYYmmddHHiiss" ["recall_telephone"]=> string(11) "9301500xxxx" } } }
I have already added the parameters as indicated by the schema, but it doesn’t seem to work. I’m still getting the same error.This is my code:
function send_to_external_api( $record, $handler ) {
$form_name = $record->get_form_settings( 'form_name' );
if ( 'Testing' !== $form_name ) {
return;
}
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
$body = array(
'customer_name' => $fields['customer_name'],
'customer_last_name' => $fields['customer_last_name'],
'id_type' => $fields['id_type'],
'customer_id' => $fields['customer_id'],
'age' => $fields['age'],
'gender' => $fields['gender'],
'country' => $fields['country'],
'state' => '',
'city' => '',
'zone' => '',
'address' => '',
'opt1' => '',
'opt2' => '',
'opt3' => '',
'opt4' => '',
'opt5' => '',
'opt6' => '',
'opt7' => '',
'opt8' => '',
'opt9' => '',
'opt10' => '',
'opt11' => '',
'opt12' => '',
'tel1' => '',
'tel2' => '',
'tel3' => '',
'tel4' => '',
'tel5' => '',
'tel6' => '',
'tel7' =>'',
'tel8' => '',
'tel9' => '',
'tel10' => '',
'tel_extra' => '',
'email' => '',
'recall_date' => $fields['recall_date'],
'recall_telephone' => $fields['recall_telephone']
);
$response = wp_remote_post('', [
'headers' => [
'wolkvox-token' => '',
'Content-Type' => 'application/json'
],
'body' => json_encode($body),
'redirection' => 10,
'httpversion' => '1.1',
]);
error_log(print_r($body, true));
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
} else {
$body = wp_remote_retrieve_body( $response );
$decoded_response = json_decode( $body, true );
var_dump( $decoded_response );
}
}
add_action( 'elementor_pro/forms/new_record', 'send_to_external_api', 10, 2 );
According to Wolkvox, there shouldn't be any issue with leaving those parameters empty.
Any help would be greatly appreciated. Thanks in advance!