Magento REST API - 如何确定 API 基础 URL?

Magento REST API - How to determine the API base URL?

我是 运行 Magento 2.2.5,我无法弄清楚 URL 是用来发出 API 请求的。例如,要获取国家/地区列表,我曾看到使用了以下语法:

/rest/default/V1/directory/countries

它在某些商店中有效,但我收到此错误:

{
"message" : "Specified request cannot be processed.",
"trace" : null
}

我开始尝试使用 URL 格式并删除了 "default" 并改用它:

/rest/V1/directory/countries

然后请求成功了。如何确定用于 Magento REST API 请求的基础 URL?到目前为止我还没有找到这个文档。

Magento 2 Api 的语法是

http://<:host:>/rest/<:store_code:>/<:api_path:>

此处 store_code 可以是您的 Magento2 实例的 "store view" 中的任何一个。您可以在管理面板中获取商店代码。您可以使用 store_code 通过 API 获取特定于该商店视图的信息。默认情况下,Magento2 安装附带 1 个网站、1 个商店和 1 个商店视图(此 store-view 在您的代码中有代码“default”情况下这可能已经改变因此你得到错误)。

架构可以借助图像来理解 Reference Image 参考:https://docs.magento.com/m2/ce/user_guide/stores/websites-stores-views.html

  • 当您指定 store_code 时,它会检查特定 Store-View 的信息。如果在数据库中没有找到指定的store_code,Apireturns"Specified request cannot be processed."消息

  • 当你不指定store_code时,它首先查找默认的Website,然后查找默认的Store对于此网站,然后找到与此商店关联的默认 商店视图 。最后它 returns 信息默认 Store-View。可以从管理面板更改默认商店和 store-view 的关联。 Reference Image

因此,API 的 URL 应该在下方,以防您需要商店特定信息。 store_code 可以设置为 all 如果您需要所有商店的信息。

http://<:host:>/rest/<:store_code:>/<:api_path:>