<p>I think you're going to have to go up the chain to where the request starts - i.e. the server config. Rewrite all urls matching <code>api?action=...</code> to somewhere else. If you're using nginx it'd be something like</p>

<pre><code>rewrite /api/$arg_action last;

</code></pre>

CC BY-SA 4.0
2020-02-27T12:19:49.417Z
2020-02-27T12:19:49.417Z
James Smith
<p>I've been tasked with recreating an API within our Craft 2.X installation. For whatever reason, the API specifies the resource it is requesting with the query string parameter <code>action</code>. For example, instead of <code>baseurl.com/api/users</code>, they have it set up as <code>baseurl.com/api?action=users</code></p>

<p>I thought I'd be okay if I used the ElementAPI plugin, set up an endpoint, and parsed the query string to figure out which set of elements to return as JSON. But it turns out that <code>action</code> is a very special query string parameter to Craft. When it receives a request containing <code>action</code>, Craft goes looking for a controller that will match it. I believe that Craft uses this pattern to handle a lot of basic business logic for the CMS. This seems like a very difficult branch of code to subvert -- this check fires before just about everything else.</p>

<p>Things I've tried, without luck:</p>

<ul>
<li><p>Changing the action trigger in the config file, in hopes of removing the magic from "action".</p></li>
<li><p>Setting up a route in routes.php that goes directly to a plugin. Didn't work because UrlManager only fires after processActionRequest, which is the source of the error.</p></li>
<li><p>Asking the people who maintain the API to change the endpoints they're looking for.</p></li>
</ul>

<p>Assuming that I'm required to use "action" as a key in my query string, how can I accomplish that in Craft 2?</p>

<p>Thanks in advance for any advice.</p>

<craft2><querystring>
Using action as a key in a query string
# Using action as a key in a query string

(Asked by: username_0 on 2020-02-26)

I've been tasked with recreating an API within our Craft 2.X installation. For whatever reason, the API specifies the resource it is requesting with the query string parameter `action`. For example, instead of `baseurl.com/api/users`, they have it set up as `baseurl.com/api?action=users`

I thought I'd be okay if I used the ElementAPI plugin, set up an endpoint, and parsed the query string to figure out which set of elements to return as JSON. But it turns out that `action` is a very special query string parameter to Craft. When it receives a request containing `action`, Craft goes looking for a controller that will match it. I believe that Craft uses this pattern to handle a lot of basic business logic for the CMS. This seems like a very difficult branch of code to subvert -- this check fires before just about everything else.

Things I've tried, without luck:

* Changing the action trigger in the config file, in hopes of removing the magic from "action".
* Setting up a route in routes.php that goes directly to a plugin. Didn't work because UrlManager only fires after processActionRequest, which is the source of the error.
* Asking the people who maintain the API to change the endpoints they're looking for.

Assuming that I'm required to use "action" as a key in my query string, how can I accomplish that in Craft 2?

Thanks in advance for any advice.

**Tags:** craft2, querystring

---

## 1 answers

### Answer by username_1 on 2020-02-27. Score: 1 | Accepted answer

I think you're going to have to go up the chain to where the request starts - i.e. the server config. Rewrite all urls matching api?action=... to somewhere else. If you're using nginx it'd be something like
rewrite /api/$arg_action last;


---

