I've GET & POST API which i need to implement in wp website. My goal is, using jetEngine Plugin, i wanna send POST request & make returned response visible in my page using jetEngine Lising Grid.
Here's an example to understand better. It's a POST req API.
API:
https://example.com/data
API_KEY:
AuthorizeKey: 'someKey-123'
BODY: User Input
{"fromDate": "1970-01-01T00:00:00.000Z",
"toDate": "2024-02-06T00:00:00.000Z", //always will be current date
"pageNo": 0,
"pageSize": 500,
"waitForResponse": true}
Returned response from server:
{
"Group": null,
"Payload": {
"Items": [
{
"Id": "bfefa53315d8379",
"CountryName": "Canada",
"Authors": []
},
{
"Id": "9aaef8c1e82",
"CountryName": "USA",
"Authors": []
}
]
}
}
According to a Github Post, it says,
Out of the box, JetEngine cannot send POST requests to get data from endpoint.
However, they provided some code snippet to how to implement POST request method. But TBH, i find it hard to understand where to put all of these code & implement it.
What i did till now
- created a user UI where user can select which type
request. If POST is selected then user can addbody.
<cx-vui-select
label="<?php _e( 'Request Method', 'jet-engine' ); ?>"
description="<?php _e( 'Select the HTTP request method.', 'jet-engine' ); ?>"
:wrapper-css="[ 'equalwidth' ]"
size="fullwidth"
:options-list="[
{ value: 'get', label: '<?php _e( 'GET', 'jet-engine' ); ?>' },
{ value: 'post', label: '<?php _e( 'POST', 'jet-engine' ); ?>' }
]"
v-model="settings.request_method"
></cx-vui-select>
<cx-vui-textarea
v-if="settings.request_method === 'post'"
label="<?php _e( 'Request Body', 'jet-engine' ); ?>"
description="<?php _e( 'Enter the JSON body for the POST request.', 'jet-engine' ); ?>"
:wrapper-css="[ 'equalwidth' ]"
size="fullwidth"
v-model="settings.request_body"
></cx-vui-textarea>
Note: request_method & request_body along with custom_headers is being saved in database.
- added this code on top outside
Requestclass inrequest.phpfile. I don't know if i'm doing it wrong or not.
add_filter( 'jet-engine/rest-api-listings/request/type', function( $type, $request ) {
$endpoint = $request->get_endpoint();
if ( false !== strpos( $endpoint['request_method'], 'post' ) ) {
$type = 'post';
}
return $type;
}, 0, 2 );
- Added this to
send_request()
public function send_request( $query_args = array(), $type = 'get' ) {
do_action( 'jet-engine/rest-api-listings/request/before-send', $this );
$type = apply_filters( 'jet-engine/rest-api-listings/request/type', $type, $this );
$args = isset( $this->endpoint['args'] ) ? $this->endpoint['args'] : array();
if ( ! isset( $args['timeout'] ) ) {
$args['timeout'] = 30;
}
if ( 'post' === $type && ! empty( $this->endpoint['request_body'] ) ) {
$args['body'] = json_decode( $this->endpoint['request_body'], true );
}
$args = apply_filters( 'jet-engine/rest-api-listings/request/args', $args, $this );
$query_args = apply_filters( 'jet-engine/rest-api-listings/request/query-args', $query_args, $this );
$url = $this->get_url();
if ( ! empty( $query_args ) ) {
if ( is_array( $query_args ) ) {
$url = add_query_arg( $query_args, $url );
} else {
$url = trailingslashit( $url ) . $query_args;
}
}
switch ( $type ) {
case 'post':
return wp_remote_post( $url, $args );
default:
return wp_remote_get( $url, $args );
}
}
It's not working, when i try to connect, jetEngine says Bad Request. I believe i need modified other code also, but i don't know how & what.
Now my query is,
How can i make a POST request using these data ? Or, where & how should i add these code snippet to work.
- Please let me know if i need to provide more code.
