<p>Allright, after a lot of digging into my code I finally found out what went wrong.</p>

<p>I used this code to clear the placeholder in my input when clicking inside them:</p>

<pre><code>$(".some-class input").val("");
</code></pre>

<p>But .<code>some-class</code> was the parent class of my form, which means I probably erased all values of hidden fields that Craft adds on runtime too. Fields such as <code>&lt;input type="hidden" name="CRAFT_CSRF_TOKEN" value=""&gt;</code> on which I also got an error. Still don't know about the "code param" but probably it was added somewhere later on runtime too.</p>

<p>After I made the selector a bit more specific, so it only selects all input elements that can be edited on the front-end, it works!</p>

CC BY-SA 3.0
2017-01-29T15:52:35.540Z
2017-01-29T15:52:35.540Z
Tom De Smet
<p>I just installed Craft fresh, there's almost nothing in my DB as I'm just beginning to develop a plugin. All I want is to save a new entry from the frontend of my website. I've done this before in other projects, but now I just can't get it to work.</p>

<p>I always receive the following error:</p>

<pre><code>Bad Request

POST param “code” doesn’t exist.
</code></pre>

<p>No other errors appear, no php error log. I do not have a param or field "code" anywhere, so I don't understad where that comes from.
The problem happens before the data is sent to my controller action as I never get into that action (I added a Craft::dd("Test") in the function with no results).</p>

<p>If I browse directly to my action controller, the function does get executed, so the path is correct. My plugin names are all correct also.</p>

<p>If I add CSRF token verification, I get an error that it could not be verified.</p>

<p>If I change the method POST to GET for testing purposes only, I get the same error but instead of paaram "code" it's param "id" that doesn't exist. I don't know what ID or CODE it's talking about.</p>

<p>I checked it with the working plugin I wrote for another project and I can't see any difference. I tested it localy with EasyPhP and also on an online devlopment server on Siteground, so two different environments as I thought it was maybe an issue with my local server. However, both had the same issues.</p>

<p>I'm at a loss, I have no idea why this will not work. Please help!</p>

<p><strong>-- UPDATE --</strong></p>

<p>I got this stack trace from the craft.log. Is this enough?
It's talking a lot about users I see... I can't connect the dots yet...</p>

<pre><code>2017/01/10 19:18:56 [error] [exception.Craft\HttpException.400] Craft\HttpException: POST param “code” doesn’t exist. in C:\gitProjects\BUT\but-project-history\craft\app\services\HttpRequestService.php:509
Stack trace:
#0 C:\gitProjects\BUT\but-project-history\craft\app\controllers\UsersController.php(333): Craft\HttpRequestService-&gt;getRequiredPost('code')
#1 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\actions\CInlineAction.php(49): Craft\UsersController-&gt;actionSetPassword()
#2 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(308): CInlineAction-&gt;runWithParams(Array)
#3 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(286): CController-&gt;runAction(Object(CInlineAction))
#4 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(265): CController-&gt;runActionWithFilters(Object(CInlineAction), Array)
#5 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CWebApplication.php(282): CController-&gt;run('setpassword')
#6 C:\gitProjects\BUT\but-project-history\craft\app\etc\web\WebApp.php(817): CWebApplication-&gt;runController('users/setpasswo...')
#7 C:\gitProjects\BUT\but-project-history\craft\app\etc\web\WebApp.php(287): Craft\WebApp-&gt;_processActionRequest()
#8 C:\gitProjects\BUT\but-project-history\craft\app\framework\base\CApplication.php(185): Craft\WebApp-&gt;processRequest()
#9 C:\gitProjects\BUT\but-project-history\craft\app\index.php(62): CApplication-&gt;run()
#10 C:\gitProjects\BUT\but-project-history\public\index.php(19): require_once('C:\\gitProjects\\...')
#11 {main}
REQUEST_URI=/
HTTP_REFERER=http://local.butprojecthistory.dev/
</code></pre>

<p><strong>-- UPDATE 2 --</strong></p>

<p>.htaccess</p>

<pre><code>&lt;IfModule mod_rewrite.c&gt;
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

    # SSL in .htaccess - Redirect to https://
    # RewriteCond %{SERVER_PORT} 80
    # RewriteRule ^(.*)$ https://site.com/$1 [R,L]
&lt;/IfModule&gt;
</code></pre>

<p><strong>-- UPDATE 3 --</strong></p>

<p>If I add the path to my controller in the actions attribute of my form tag (but with the "actions" segment in front of the path) than I also get to the controller, if I leave it blank and use a hidden input field with an action attribute so I would get redirected to the same page, it gives the error. Does that clear something up for anybody?</p>

<p><strong>-- UPDATE 4 --</strong></p>

<p>Traced it back to my javascript. Started using/learning Typescript recently, so there must be something wrong with that...</p>

CC BY-SA 3.0
2017-01-10T00:34:49.823Z
Can you enable devMode and share the full stack trace leading up that "Bad Request" error message?
Brad Bell
CC BY-SA 3.0
2017-01-10T19:24:32.087Z
As requested. Edited my original question with the stack trace.
Tom De Smet
CC BY-SA 3.0
2017-01-11T02:02:08.357Z
My guess is that your `.htaccess` file (or nginx conf file) is stripping off querystring parameters and not letting them pass through in a rewrite rule.  For those `setpassword` requests, Craft passes `code` in through the querystring.
Brad Bell
CC BY-SA 3.0
2017-01-13T14:44:18.943Z
I added my .htaccess file, doesn't look like anything special to me.
Tom De Smet
CC BY-SA 3.0
2017-01-13T22:25:13.590Z
Move your SSL rewrite lines to above the Craft ones and see if that helps.
Brad Bell
CC BY-SA 3.0
2017-01-15T08:36:03.617Z
Those lines are in comment so they are not executed. I've removed them completely now but that didn't help either...
Tom De Smet
<plugin-development><form><request><csrf><post>
Bad Request with weird params
# Bad Request with weird params

(Asked by: username_0 on 2017-01-09)

I just installed Craft fresh, there's almost nothing in my DB as I'm just beginning to develop a plugin. All I want is to save a new entry from the frontend of my website. I've done this before in other projects, but now I just can't get it to work.
I always receive the following error:
Bad Request

POST param “code” doesn’t exist.

No other errors appear, no php error log. I do not have a param or field "code" anywhere, so I don't understad where that comes from.
The problem happens before the data is sent to my controller action as I never get into that action (I added a Craft::dd("Test") in the function with no results).
If I browse directly to my action controller, the function does get executed, so the path is correct. My plugin names are all correct also.
If I add CSRF token verification, I get an error that it could not be verified.
If I change the method POST to GET for testing purposes only, I get the same error but instead of paaram "code" it's param "id" that doesn't exist. I don't know what ID or CODE it's talking about.
I checked it with the working plugin I wrote for another project and I can't see any difference. I tested it localy with EasyPhP and also on an online devlopment server on Siteground, so two different environments as I thought it was maybe an issue with my local server. However, both had the same issues.
I'm at a loss, I have no idea why this will not work. Please help!
-- UPDATE --
I got this stack trace from the craft.log. Is this enough?
It's talking a lot about users I see... I can't connect the dots yet...
2017/01/10 19:18:56 [error] [exception.Craft\HttpException.400] Craft\HttpException: POST param “code” doesn’t exist. in C:\gitProjects\BUT\but-project-history\craft\app\services\HttpRequestService.php:509
Stack trace:
#0 C:\gitProjects\BUT\but-project-history\craft\app\controllers\UsersController.php(333): Craft\HttpRequestService->getRequiredPost('code')
#1 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\actions\CInlineAction.php(49): Craft\UsersController->actionSetPassword()
#2 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(308): CInlineAction->runWithParams(Array)
#3 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(286): CController->runAction(Object(CInlineAction))
#4 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
#5 C:\gitProjects\BUT\but-project-history\craft\app\framework\web\CWebApplication.php(282): CController->run('setpassword')
#6 C:\gitProjects\BUT\but-project-history\craft\app\etc\web\WebApp.php(817): CWebApplication->runController('users/setpasswo...')
#7 C:\gitProjects\BUT\but-project-history\craft\app\etc\web\WebApp.php(287): Craft\WebApp->_processActionRequest()
#8 C:\gitProjects\BUT\but-project-history\craft\app\framework\base\CApplication.php(185): Craft\WebApp->processRequest()
#9 C:\gitProjects\BUT\but-project-history\craft\app\index.php(62): CApplication->run()
#10 C:\gitProjects\BUT\but-project-history\public\index.php(19): require_once('C:\\gitProjects\\...')
#11 {main}
REQUEST_URI=/
HTTP_REFERER=http://local.butprojecthistory.dev/

-- UPDATE 2 --
.htaccess

    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

    # SSL in .htaccess - Redirect to https://
    # RewriteCond %{SERVER_PORT} 80
    # RewriteRule ^(.*)$ https://site.com/$1 [R,L]


-- UPDATE 3 --
If I add the path to my controller in the actions attribute of my form tag (but with the "actions" segment in front of the path) than I also get to the controller, if I leave it blank and use a hidden input field with an action attribute so I would get redirected to the same page, it gives the error. Does that clear something up for anybody?
-- UPDATE 4 --
Traced it back to my javascript. Started using/learning Typescript recently, so there must be something wrong with that...

**Tags:** plugin-development, form, request, csrf, post

> **Comments:**
> **username_1**: Can you enable devMode and share the full stack trace leading up that "Bad Request" error message?  
> **username_0**: As requested. Edited my original question with the stack trace.  
> **username_1**: My guess is that your `.htaccess` file (or nginx conf file) is stripping off querystring parameters and not letting them pass through in a rewrite rule.  For those `setpassword` requests, Craft passes `code` in through the querystring.  
> **username_0**: I added my .htaccess file, doesn't look like anything special to me.  
> **username_1**: Move your SSL rewrite lines to above the Craft ones and see if that helps.  
> **username_0**: Those lines are in comment so they are not executed. I've removed them completely now but that didn't help either...  

---

## 1 answers

### Answer by username_0 on 2017-01-29. Score: 1 | Accepted answer

Allright, after a lot of digging into my code I finally found out what went wrong.
I used this code to clear the placeholder in my input when clicking inside them:
$(".some-class input").val("");

But .some-class was the parent class of my form, which means I probably erased all values of hidden fields that Craft adds on runtime too. Fields such as  on which I also got an error. Still don't know about the "code param" but probably it was added somewhere later on runtime too.
After I made the selector a bit more specific, so it only selects all input elements that can be edited on the front-end, it works!


---

