Sending post request with wp_remote_post not working correctly
I need to submit a form on the woo-commerce
product
page via restful API
custom endpoint
the problem is the wp_remote_post
not working correctly with me or me mostly I did something wrong.
The form is add to cart
form and it has a lot of custom data, not the usual add to cart. for plugin called yith composite to specific
My code so far
add_action('rest_api_init', function () {
register_rest_route( 'yith-composite', 'add-item',array(
'methods' = 'POST',
'callback' = 'yith_custom_add_item'
));
});
function yith_custom_add_item( WP_REST_Request $request ) {
$currentuserid_fromjwt = get_current_user_id();
$product_id = $_GET['id'];
$url = get_permalink( $product_id );
$user_cookie = wp_generate_auth_cookie($currentuserid_fromjwt, time() + (10 * 365 * 24 * 60 * 60), 'logged_in');
$response = wp_remote_post( $url, array(
'body' = json_encode($request-get_json_params()['body']),
'headers' = array(
'Content-Type' = 'multipart/form-data;',
'cookie' = 'PHPSESSID=2f3f5db725c728c627ada8196ca9d438; tk_ai=woo%3A4404G2GnKIS%2BNq44234vMsYl; wordpress_test_cookie=WP+Cookie+check; wp-settings-time-138=1574607034; wordpress_logged_in_856ec7e7dd32c9b2fc11b366505cf40d=' . $user_cookie
),
) );
return $response;
exit;
}
I send the following body including bearer token with postman:
{
"body": {
"ywcp_selection[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": -1,
"ywcp_variation_id[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 124136,
"ywcp_quantity[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 1,
"ywcp_selected_product_value[153CE4EE-C1A0-6D39-9AD5-58B51C5AB306]": 10,
"ywcp_selection[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": -1,
"attribute_pa_sphere[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": "5-75",
"ywcp_variation_id[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 110359,
"ywcp_quantity[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 1,
"ywcp_selected_product_value[471BAD03-9C3B-B44E-8584-348A5F33F8A6]": 102552,
"quantity": 1,
"add-to-cart": 102491
}
}
Topic wp-remote-post api plugin-development Wordpress
Category Web