如何在WordPress中通过JetEngine REST API实现POST请求?
在WordPress中通过JetEngine实现POST API请求并展示响应
需求概述
需要在WordPress站点中借助JetEngine插件发送POST请求到指定API,并通过Listing Grid组件展示返回的响应数据。JetEngine默认不支持通过POST请求获取端点数据,参考GitHub提供的代码片段修改后,出现「Bad Request」错误,无法正常工作。
API详情
- API地址:
https://example.com/data - 授权密钥:
AuthorizeKey: 'someKey-123' - 请求体(用户输入):
{"fromDate": "1970-01-01T00:00:00.000Z", "toDate": "2024-02-06T00:00:00.000Z", // 需设置为当前日期 "pageNo": 0, "pageSize": 500, "waitForResponse": true}
- 服务器响应示例:
{ "Group": null, "Payload": { "Items": [ { "Id": "bfefa53315d8379", "CountryName": "Canada", "Authors": [] }, { "Id": "9aaef8c1e82", "CountryName": "USA", "Authors": [] } ] } }
已完成操作
- 创建请求方式选择UI:添加POST请求体输入框,相关设置
request_method、request_body、custom_headers已保存到数据库。
<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>
- 添加请求类型过滤钩子:在
request.php的Request类外部添加代码:
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 );
- 修改
send_request()方法:调整request.php中的该方法以支持POST请求:
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 ); } }
当前问题
修改后JetEngine提示「Bad Request」,无法成功获取API响应数据,不确定还需调整哪些代码。
解决方案建议
1. 正确处理请求头
API需要AuthorizeKey授权头,同时POST请求需指定Content-Type: application/json,需在请求参数中添加这些头信息:
add_filter( 'jet-engine/rest-api-listings/request/args', function( $args, $request ) { $endpoint = $request->get_endpoint(); // 仅针对POST请求添加头信息 if ( 'post' === $request->get_request_type() ) { // 设置Content-Type $args['headers']['Content-Type'] = 'application/json'; // 添加授权密钥 $args['headers']['AuthorizeKey'] = 'someKey-123'; // 如果custom_headers已保存到数据库,可替换为动态获取 // if ( !empty( $endpoint['custom_headers'] ) ) { // $args['headers'] = array_merge( $args['headers'], $endpoint['custom_headers'] ); // } } return $args; }, 10, 2 );
2. 调整请求体处理逻辑
部分API要求直接接收JSON字符串而非数组,将json_decode改为直接使用原字符串:
// 替换原send_request方法中对应代码 if ( 'post' === $type && ! empty( $this->endpoint['request_body'] ) ) { // 直接使用JSON字符串作为请求体 $args['body'] = $this->endpoint['request_body']; }
3. 修正请求类型过滤逻辑
确保过滤函数准确识别POST请求设置:
add_filter( 'jet-engine/rest-api-listings/request/type', function( $type, $request ) { $endpoint = $request->get_endpoint(); // 直接判断request_method的值是否为post if ( isset( $endpoint['request_method'] ) && 'post' === strtolower( $endpoint['request_method'] ) ) { $type = 'post'; } return $type; }, 10, 2 );
4. 添加调试日志排查问题
在send_request()方法中添加日志,查看实际发送的请求参数和返回错误:
// 在wp_remote_post之前添加 error_log( 'JetEngine POST Request Args: ' . print_r( $args, true ) ); error_log( 'JetEngine POST Request URL: ' . $url ); // 获取响应后添加 $response = wp_remote_post( $url, $args ); error_log( 'JetEngine POST Response: ' . print_r( $response, true ) ); return $response;
可通过WordPress根目录下的wp-content/debug.log查看日志(需开启WP_DEBUG)。
5. 避免修改JetEngine核心文件
将所有自定义代码放到子主题的functions.php中,或创建独立的自定义插件,避免JetEngine更新后代码失效。例如,通过钩子重写send_request逻辑,而非直接修改核心文件:
add_action( 'jet-engine/rest-api-listings/request/before-send', function( $request ) { // 动态修改request对象的属性或添加逻辑 $endpoint = $request->get_endpoint(); if ( 'post' === $endpoint['request_method'] ) { // 在这里处理请求体和头信息 } } );
内容的提问来源于stack exchange,提问作者MH Prince
相关产品推荐
相关产品推荐

