3 added 144 characters in body
source | link

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

UPDATE

You can use wp_remote_post() to make the external API request from the foo() function; example:

function foo() {
    if ( empty( $_POST['data'] ) ) {
        wp_die( 'Missing data' );
    }

    $post_data = wp_unslash( $_POST['data'] );
    $post_data['command'] = wp_unslash( $_POST['command'] );
    $post_data['request_token_id'] = wp_unslash( $_POST['request_token_id'] );

    $api_url = 'https://apitest.sendyit.com/v1/';
 
    $response = wp_remote_post( $api_url, array(
        // Set the Content-Type header.
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
        // Set the request payload/body.
        'body'    => json_encode( $post_data ),
    ) );

    $res_body = wp_remote_retrieve_body( $response );
    echo $res_body;

    wp_die();
}

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

UPDATE

You can use wp_remote_post() to make the external API request from the foo() function; example:

function foo() {
    if ( empty( $_POST['data'] ) ) {
        wp_die( 'Missing data' );
    }

    $post_data = wp_unslash( $_POST['data'] );
    $api_url = 'https://apitest.sendyit.com/v1/';
 
    $response = wp_remote_post( $api_url, array(
        // Set the Content-Type header.
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
        // Set the request payload/body.
        'body'    => json_encode( $post_data ),
    ) );

    $res_body = wp_remote_retrieve_body( $response );
    echo $res_body;

    wp_die();
}

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

UPDATE

You can use wp_remote_post() to make the external API request from the foo() function; example:

function foo() {
    if ( empty( $_POST['data'] ) ) {
        wp_die( 'Missing data' );
    }

    $post_data = wp_unslash( $_POST['data'] );
    $post_data['command'] = wp_unslash( $_POST['command'] );
    $post_data['request_token_id'] = wp_unslash( $_POST['request_token_id'] );

    $api_url = 'https://apitest.sendyit.com/v1/';
    $response = wp_remote_post( $api_url, array(
        // Set the Content-Type header.
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
        // Set the request payload/body.
        'body'    => json_encode( $post_data ),
    ) );

    $res_body = wp_remote_retrieve_body( $response );
    echo $res_body;

    wp_die();
}
2 added 791 characters in body
source | link

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

UPDATE

You can use wp_remote_post() to make the external API request from the foo() function; example:

function foo() {
    if ( empty( $_POST['data'] ) ) {
        wp_die( 'Missing data' );
    }

    $post_data = wp_unslash( $_POST['data'] );
    $api_url = 'https://apitest.sendyit.com/v1/';

    $response = wp_remote_post( $api_url, array(
        // Set the Content-Type header.
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
        // Set the request payload/body.
        'body'    => json_encode( $post_data ),
    ) );

    $res_body = wp_remote_retrieve_body( $response );
    echo $res_body;

    wp_die();
}

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});

UPDATE

You can use wp_remote_post() to make the external API request from the foo() function; example:

function foo() {
    if ( empty( $_POST['data'] ) ) {
        wp_die( 'Missing data' );
    }

    $post_data = wp_unslash( $_POST['data'] );
    $api_url = 'https://apitest.sendyit.com/v1/';

    $response = wp_remote_post( $api_url, array(
        // Set the Content-Type header.
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
        // Set the request payload/body.
        'body'    => json_encode( $post_data ),
    ) );

    $res_body = wp_remote_retrieve_body( $response );
    echo $res_body;

    wp_die();
}
1
source | link

You got the error 400 Bad Request because the Content-Type header is application/json:

$.ajax({
  headers:{
    "Content-Type":"application/json"
  },
  data: {
    ...
  },
  ...
});

And when the request content is a JSON string, the $_REQUEST['action'] which WordPress uses to identify the proper AJAX action (which is foo in your code) is not available (i.e. it's a NULL).

So based on your code, and with your second approach, you can just omit the headers part.

$.ajax({
  // No "headers" here.
  data: {
    ...
  },
  ...
});