Reputation: 17
I have registered a api request the following way in the code, then in postman I call that request and add some params, but when I run the api request endpoint it returns null. How do I return the data that's being sent?
/**
* This is our callback
* function that embeds our phrase in a WP_REST_Response
*/
function addProductFromCRM($data) {
//$name = $data['name'];
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response($data);
}
/**
* This function is where we register our routes for our example endpoint.
*/
function wp_register_crm_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route('crm/v1', '/addproduct/', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => 'POST',
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'addProductFromCRM',
));
}
add_action('rest_api_init', 'wp_register_crm_routes');
Upvotes: 0
Views: 1502
Reputation: 970
You can use below code snippet as per you need like on isset($_POST)
or any other callback function
. You must have the idea of your Register Route URL and must be working. you can use wp_remote_get
or wp_remote_post
as per your need. for more reference please check WordPress official site
$response = wp_remote_get("URL TO YOUR REGISTER ROUTE");
if ( is_array( $response ) ) {
$response_code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$body_data = json_decode($body);
}
Upvotes: 0
Reputation: 86
What addproduct endpoint should return? JSON? You can do something like this:
function addProductFromCRM($request) {
wp_send_json($request->get_params());
}
Upvotes: 1