问题
I'm running Magento 2.2.5 and was having trouble working out what the URL was for making API requests. For example to GET a list of countries I had seen the following syntax used:
/rest/default/V1/directory/countries
which has worked on some stores, but I was getting this error:
{
"message" : "Specified request cannot be processed.",
"trace" : null
}
I started playing around with the URL format and removed the "default" and used this instead:
/rest/V1/directory/countries
and the requests were then successful. How does one determine what the base URL to use for Magento REST API requests? I haven't been able to find this documented so far.
回答1:
The syntax of Magento 2 Api is
http://<:host:>/rest/<:store_code:>/<:api_path:>
Here store_code
can be any one of the "store view" of your Magento2 instance. You can get the store code in admin panel. You can use store_code
to get information specific to that Store View via API. By default Magento2 installation comes with 1 website, 1 store & 1 store view (this store-view has code "default", in your case this might have changed hence you get the error).
The architecture can be understand with the help of image Reference Image Ref: https://docs.magento.com/m2/ce/user_guide/stores/websites-stores-views.html
When you specify
store_code
it checks information for that particular Store-View. If the specifiedstore_code
is not found in the database, Api returns "Specified request cannot be processed." message.When you do not specify
store_code
it first finds default Website, then finds default Store for this Website & then finds default Store View Associated to this Store. And finally it returns information for this default Store-View. The association of default store & store-view can be changed from admin panel. Reference Image
Hence the URL for API should be below in case you need store specific information. store_code
can be set to all
if you need information for all the stores.
http://<:host:>/rest/<:store_code:>/<:api_path:>
来源:https://stackoverflow.com/questions/56513873/magento-rest-api-how-to-determine-the-api-base-url