简体   繁体   English

对 WP API 的 POST 请求被解释为 GET 请求

[英]POST requests to WP API are interpreted as GET requests

I'm trying to create an order from Woocommerce API and it is not working as intended: the request (sent as POST) is returning all orders (like it would be a GET request), instead of creating a new one.我正在尝试从 Woocommerce API 创建一个订单,但它没有按预期工作:请求(作为 POST 发送)正在返回所有订单(就像它是一个 GET 请求),而不是创建一个新订单。 The reald odd thing is that the same exact request is working on the pre-production server but not on the production server.真正奇怪的是,相同的请求在预生产服务器上运行,但不在生产服务器上运行。

This seems to be a global issue with the API, as other requests (like creating a post from the WP API) are not working, except the POST request used to get the access token.这似乎是 API 的一个全局问题,因为除了用于获取访问令牌的 POST 请求之外,其他请求(例如从 WP API 创建帖子)都不起作用。

Here is the request I send as POST:这是我作为 POST 发送的请求:

curl -X POST https://www.domain.tld/wp-json/wc/v2/orders?access_token=... \
-H "Content-Type: application/json" \
-d '{
     "customer_id": "1",
     "payment_method": "app",
     "payment_method_title": "Test payment",
     "set_paid": false,
     "billing": {
         "first_name": "test",
         "last_name": "test",
         "address_1": "test",
         "address_2": "test",
         "city": "test",
         "postcode": "00000",
         "country": "FR",
         "phone": "0123456789",
         "email": "test@test.tld"
     },
     "shipping": {
         "first_name": "test",
         "last_name": "test",
         "address_1": "test",
         "address_2": "test",
         "city": "test",
         "postcode": "00000",
         "country": "FR",
         "phone": "0123456789",
         "email": "test@test.tld"
     },
     "shipping_lines": [
         {
             "method_id": "livraison_gratuite",
             "method_title": "Livraison gratuite",
             "total": 0
         }
     ],
     "line_items": [
         {
             "product_id": 302,
             "variation_id": 589,
             "quantity": 1
         },
         {
             "product_id": 798,
             "quantity": 1
         }
     ]
 }'

Again the same request is working on pre-production server so I don't think the problem is related to the request itself.同样的请求在预生产服务器上工作,所以我认为问题与请求本身无关。

Here is the return I get in postman for this request on production server:这是我在邮递员中针对生产服务器上的此请求获得的回报:

在此处输入图片说明

I eliminated all the potentiel causes:我消除了所有潜在的原因:

  • Both websites are using the same plugins, Wordpress version and plugins are both up to date,两个网站都使用相同的插件,Wordpress 版本和插件都是最新的,
  • Both websites are using https,两个网站都使用https,
  • Cache plugin has been deactivated,缓存插件已停用,
  • API settings in Woocommerce and WP Oauth Server are identical, Woocommerce 和 WP Oauth Server 中的 API 设置是相同的,
  • User used for sending the request (identified with the access token provided by WP Oauth Server) is admin,用于发送请求的用户(由 WP Oauth Server 提供的访问令牌标识)是 admin,
  • Server configuration is the same so far as I know (PHP7).据我所知,服务器配置是相同的(PHP7)。

I'm running out of idea about why this is happening.我不知道为什么会发生这种情况。 Anyone have a clue about what could cause this?任何人都知道可能导致这种情况的原因?

I finally found out what was happening here.我终于知道这里发生了什么。 There is a redirection rule on the production server that add the trailing slash to URL when it is missing.生产服务器上有一个重定向规则,当它丢失时,将尾部斜杠添加到 URL。 This was causing the request recognized as GET instead of POST (POST data are not sent when HTTP request is redirected).这导致请求被识别为 GET 而不是 POST(重定向 HTTP 请求时不发送 POST 数据)。

Adding a trailing slash fixed the issue:添加尾部斜杠解决了该问题:

curl -X POST https://www.domain.tld/wp-json/wc/v2/orders/?access_token=...

As a note, this can be used as well on the pre-production server that doesn't use any redirect rule of this kind, so always adding a trailing slash to POST API requests can be a good idea for not stumbling upon this kind of issue.请注意,这也可以在不使用任何此类重定向规则的预生产服务器上使用,因此始终为 POST API 请求添加尾部斜杠可能是一个好主意,以免绊倒此类问题。

An alternative solution can be to add a RewriteCond of this kind to prevent the redirection for POST requests:另一种解决方案是添加这种类型的 RewriteCond 以防止 POST 请求重定向:

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteCond %{THE_REQUEST} !POST
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM