rest api - How do you format the set_body option for WP_Rest_Request?

I am looking to pull data from a Google Spreadsheet using SpreadAPI. This is my current code. $request = new WP_REST_Req

I am looking to pull data from a Google Spreadsheet using SpreadAPI.

This is my current code.

$request = new WP_REST_Request('POST', '');

$request->setBody('{\n"method": "GET",\n"sheet": "date",\n"key": "ACCESS_KEY"\n}');

$response = rest_do_request( $request );

However I'm clearly doing something wrong when it comes to formatting the setBody option. I'm not getting a json response, and I can't seem to find any examples of this in the WordPress documentation.

The call works fine when I put it into the RAW section in Postman like this. I'm just not sure how to do that using WordPress.

{
"method": "GET",
"sheet": "date",
"key": "ACCESS_KEY"
}

I am looking to pull data from a Google Spreadsheet using SpreadAPI.

This is my current code.

$request = new WP_REST_Request('POST', 'https://script.google/macros/s/UNIQUE_CODE/exec');

$request->setBody('{\n"method": "GET",\n"sheet": "date",\n"key": "ACCESS_KEY"\n}');

$response = rest_do_request( $request );

However I'm clearly doing something wrong when it comes to formatting the setBody option. I'm not getting a json response, and I can't seem to find any examples of this in the WordPress documentation.

The call works fine when I put it into the RAW section in Postman like this. I'm just not sure how to do that using WordPress.

{
"method": "GET",
"sheet": "date",
"key": "ACCESS_KEY"
}
Share Improve this question asked Feb 5, 2020 at 21:23 Brendan QuigleyBrendan Quigley 131 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

WP_Rest_Request is not a way to make outgoing calls to remote REST APIs, it's a data object core uses to pass around information about an incoming request. It's basically something you recieved, it isn't something you can send.

If you poke your sites REST API with a request, WordPress creates and populates a WP_Rest_Request object and uses it to help handle the request.

So you cannot use this class to make requests to Google.

For that, you want to use the WP_HTTP APIs, such as wp_remote_get, or wp_remote_post.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773962a4592932.html

相关推荐

  • rest api - How do you format the set_body option for WP_Rest_Request?

    I am looking to pull data from a Google Spreadsheet using SpreadAPI. This is my current code. $request = new WP_REST_Req

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信