Answers.Body: <p>There's no <code>setRequestType()</code> method or the like, no. The <a href="https://craftcms.com/classreference/services/HttpRequestService#isCpRequest-detail" rel="nofollow noreferrer">HttpRequestService</a> will consider any request with the <code>cpTrigger</code> in its URL a control panel request, and I don't see any easy way around that.</p>
  
  <p>It's a bit convoluted, but what you could do is to wrap whatever code you're currently executing in your <code>entries.onSaveEntry</code> in a controller action method, and then use <a href="http://guzzle3.readthedocs.io/docs.html" rel="nofollow noreferrer">Guzzle</a> in your <code>onSaveEntry</code> event handler to trigger a <em>site</em> request (i.e. a request <em>without</em> the <code>cpTrigger</code> in the URL) to that action.</p>
  
  <p>Here's how your controller action could look:</p>
  
  <pre><code>&lt;?
  namespace Craft;
  
  class MyPluginController extends BaseController
  {
  
      protected $allowAnonymous = ['actionGenerateIndex'];
  
      public function actionGenerateIndex ()
      {
  
          // Get entry (based on a ?entryId URL parameter
          $entryId = craft()-&gt;request-&gt;getParam('entryId');
          $entry = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, [
              'id' =&gt; $entryId,
          ])-&gt;first();
  
          // ...your code to generate the index data here
  
      }
  }
  </code></pre>
  
  <p>Here's how you could fire off a request to this action using Guzzle, stripping the <code>cpTrigger</code> from the action URL to make sure Craft treats the action as a site request:</p>
  
  <pre><code>craft()-&gt;on('entries.onSaveEntry', function (Event $e) {
  
      // Get entry
      $entry = $e-&gt;params['entry'];
  
      // Get action URL, strip the `cpTrigger` segment
      $actionUrl = UrlHelper::getActionUrl('myPlugin/generateIndex', [
          'entryId' =&gt; $entry-&gt;id,
      ]);
      $actionUrl = str_replace(craft()-&gt;config-&gt;get('cpTrigger').'/', '', $actionUrl);
  
      // Request action
      $client = new Guzzle\Http\Client();
      $request = $client-&gt;get($actionUrl, [], [
        'headers' =&gt; [
          'X-Requested-With' =&gt; 'XMLHttpRequest',
        ],
      ]);
      $request-&gt;send();
  
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-04T19:15:48.193Z
Answers.Text: Thanks @matsmikkel! Yep, a bit icky, but if I really needed to, your solution is a good one. I suppose I should make a FR for forcing the request type.
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-01T08:36:25.920Z
Answers.LastActivityDate: 2016-12-01T08:36:25.920Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>Is there a way to force a request type (<code>HttpRequestService::isCpRequest</code> vs. <code>HttpRequestService::isSiteRequest</code>)?</p>
  
  <p>In my case I'm generating some indexing data on <code>entries.onSaveEntry</code>. I'm trying to use some <code>UrlHelper</code> methods, but the resulting urls are wrong, since it thinks it is a cp request.</p>
  
  <p>I have to do something like this load templates:</p>
  
  <pre><code>craft()-&gt;templates-&gt;setTemplateMode(TemplateMode::Site);
  </code></pre>
  
  <p>Is there any way to do something similar for request type?</p>
  
Tags: <plugin-development><url><request>
Title: How can I force a request type (HttpRequestService::isCpRequest vs. HttpRequestService::isSiteRequest)?
ThreadText: # How can I force a request type (HttpRequestService::isCpRequest vs. HttpRequestService::isSiteRequest)?
  
  (Asked by: username_0 on 2016-11-30)
  
  Is there a way to force a request type (`HttpRequestService::isCpRequest` vs. `HttpRequestService::isSiteRequest`)?
  
  In my case I'm generating some indexing data on `entries.onSaveEntry`. I'm trying to use some `UrlHelper` methods, but the resulting urls are wrong, since it thinks it is a cp request.
  
  I have to do something like this load templates:
  
  ```
  craft()->templates->setTemplateMode(TemplateMode::Site);
  
  ```
  
  Is there any way to do something similar for request type?
  
  **Tags:** plugin-development, url, request
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-12-01. Score: 2 | Accepted answer
  
  There's no `setRequestType()` method or the like, no. The [HttpRequestService](https://craftcms.com/classreference/services/HttpRequestService#isCpRequest-detail) will consider any request with the `cpTrigger` in its URL a control panel request, and I don't see any easy way around that.
  
  It's a bit convoluted, but what you could do is to wrap whatever code you're currently executing in your `entries.onSaveEntry` in a controller action method, and then use [Guzzle](http://guzzle3.readthedocs.io/docs.html) in your `onSaveEntry` event handler to trigger a *site* request (i.e. a request *without* the `cpTrigger` in the URL) to that action.
  
  Here's how your controller action could look:
  
  ```
  <?
  namespace Craft;
  
  class MyPluginController extends BaseController
  {
  
      protected $allowAnonymous = ['actionGenerateIndex'];
  
      public function actionGenerateIndex ()
      {
  
          // Get entry (based on a ?entryId URL parameter
          $entryId = craft()->request->getParam('entryId');
          $entry = craft()->elements->getCriteria(ElementType::Entry, [
              'id' => $entryId,
          ])->first();
  
          // ...your code to generate the index data here
  
      }
  }
  
  ```
  
  Here's how you could fire off a request to this action using Guzzle, stripping the `cpTrigger` from the action URL to make sure Craft treats the action as a site request:
  
  ```
  craft()->on('entries.onSaveEntry', function (Event $e) {
  
      // Get entry
      $entry = $e->params['entry'];
  
      // Get action URL, strip the `cpTrigger` segment
      $actionUrl = UrlHelper::getActionUrl('myPlugin/generateIndex', [
          'entryId' => $entry->id,
      ]);
      $actionUrl = str_replace(craft()->config->get('cpTrigger').'/', '', $actionUrl);
  
      // Request action
      $client = new Guzzle\Http\Client();
      $request = $client->get($actionUrl, [], [
        'headers' => [
          'X-Requested-With' => 'XMLHttpRequest',
        ],
      ]);
      $request->send();
  
  });
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks @matsmikkel! Yep, a bit icky, but if I really needed to, your solution is a good one. I suppose I should make a FR for forcing the request type.  
  
  ---
  
  