- Posts: 9
Question Post request triggers 405 error with no debug message
- jbldn1982
- Topic Author
- Offline
- New Member
Less
More
3 weeks 4 days ago #1
by jbldn1982
Post request triggers 405 error with no debug message was created by jbldn1982
Hi all. As I am redeveloping our very basic family forum using a module, all GET routes seem to work fine. It is only when I then want to post, that the site throws a 405 Error (I understand this means POST not allowed). That is strange though as I have set up the post route and handler below.
Do I have to specifically allow post requests somewhere else? Thanks a lot for the help.
/**
* Class ForumModule
*/
class ForumModule extends AbstractModule implements ModuleCustomInterface, ModuleMenuInterface, RequestHandlerInterface
{
use ModuleCustomTrait;
use ModuleMenuTrait;
use ViewResponseTrait;
protected const ROUTE_URL = '/tree/{tree}/forum';
protected const ROUTE_URL_VIEW_TOPIC = '/tree/{tree}/forum/topic/{topicId}';
protected const ROUTE_URL_ADD_TOPIC = '/tree/{tree}/forum/add-topic';
protected const ROUTE_URL_ADD_TOPIC_ACTION = '/tree/{tree}/forum/add-topic-action';
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
$routeMap = Registry::routeFactory()->routeMap();
$routeMap->get(static::class, static::ROUTE_URL, $this);
$routeMap->get(static::class . ':viewTopic', static::ROUTE_URL_VIEW_TOPIC, $this);
$routeMap->get(static::class . ':addTopic', static::ROUTE_URL_ADD_TOPIC, $this);
$routeMap->post(static::class . ':addTopicAction', static::ROUTE_URL_ADD_TOPIC_ACTION, $this);
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
And then the very basic view for now (which displays fine).
<?php
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Module\ForumModule;
?>
<h2><?= $title ?></h2>
<form method="post" action="<?= e(route(ForumModule::class, )) ?>">
<?= csrf_field() ?>
<div class="form-group">
<label for="title"><?= I18N::translate('Title') ?></label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group">
<label for="category"><?= I18N::translate('Category') ?></label>
<select class="form-control" id="category" name="category" required>
<option value=""><?= I18N::translate('Select a category') ?></option>
<?php foreach ($categories as $value => $label): ?>
<option value="<?= e($value) ?>"><?= e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="content"><?= I18N::translate('Content') ?></label>
<textarea class="form-control" id="content" name="content" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-primary">
<?= I18N::translate('Create Topic') ?>
</button>
</form>
Do I have to specifically allow post requests somewhere else? Thanks a lot for the help.
/**
* Class ForumModule
*/
class ForumModule extends AbstractModule implements ModuleCustomInterface, ModuleMenuInterface, RequestHandlerInterface
{
use ModuleCustomTrait;
use ModuleMenuTrait;
use ViewResponseTrait;
protected const ROUTE_URL = '/tree/{tree}/forum';
protected const ROUTE_URL_VIEW_TOPIC = '/tree/{tree}/forum/topic/{topicId}';
protected const ROUTE_URL_ADD_TOPIC = '/tree/{tree}/forum/add-topic';
protected const ROUTE_URL_ADD_TOPIC_ACTION = '/tree/{tree}/forum/add-topic-action';
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
$routeMap = Registry::routeFactory()->routeMap();
$routeMap->get(static::class, static::ROUTE_URL, $this);
$routeMap->get(static::class . ':viewTopic', static::ROUTE_URL_VIEW_TOPIC, $this);
$routeMap->get(static::class . ':addTopic', static::ROUTE_URL_ADD_TOPIC, $this);
$routeMap->post(static::class . ':addTopicAction', static::ROUTE_URL_ADD_TOPIC_ACTION, $this);
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
And then the very basic view for now (which displays fine).
<?php
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Module\ForumModule;
?>
<h2><?= $title ?></h2>
<form method="post" action="<?= e(route(ForumModule::class, )) ?>">
<?= csrf_field() ?>
<div class="form-group">
<label for="title"><?= I18N::translate('Title') ?></label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group">
<label for="category"><?= I18N::translate('Category') ?></label>
<select class="form-control" id="category" name="category" required>
<option value=""><?= I18N::translate('Select a category') ?></option>
<?php foreach ($categories as $value => $label): ?>
<option value="<?= e($value) ?>"><?= e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="content"><?= I18N::translate('Content') ?></label>
<textarea class="form-control" id="content" name="content" rows="5" required></textarea>
</div>
<button type="submit" class="btn btn-primary">
<?= I18N::translate('Create Topic') ?>
</button>
</form>
Please Log in or Create an account to join the conversation.
- fisharebest
- Offline
- Administrator
3 weeks 1 day ago #2
by fisharebest
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Replied by fisharebest on topic Post request triggers 405 error with no debug message
Your form is posting data to the route named "ForumModule::class".
This route is a "GET" route:
$routeMap->get(static::class, static::ROUTE_URL, $this);
Your form should probably use this route:
route(ForumModule::class . ':addTopicAction')
This route is a "GET" route:
$routeMap->get(static::class, static::ROUTE_URL, $this);
Your form should probably use this route:
route(ForumModule::class . ':addTopicAction')
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Please Log in or Create an account to join the conversation.
- jbldn1982
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
2 weeks 5 days ago #3
by jbldn1982
Replied by jbldn1982 on topic Post request triggers 405 error with no debug message
Thanks a lot Greg. That still throws a 405 error. Is it expected behaviour that I am not getting any debug output? I don't even know where this 405 error is being thrown. Many thanks!
Please Log in or Create an account to join the conversation.
- fisharebest
- Offline
- Administrator
2 weeks 5 days ago #4
by fisharebest
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Replied by fisharebest on topic Post request triggers 405 error with no debug message
Look at the HTTP requests in your browser's dev tools.
Confirm that the POST URL is the one you expect.
Confirm that the POST URL is the one you expect.
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Please Log in or Create an account to join the conversation.
- jbldn1982
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
2 weeks 5 days ago #5
by jbldn1982
Replied by jbldn1982 on topic Post request triggers 405 error with no debug message
Hi Greg - yes it is the address I am expecting. Maybe it is the mix of get and post urls that is causing issues. Do I need a separate module to handle post requests? I tried to explicitly allow posts below but to no avail.
Thanks for the help!
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
$routeMap = Registry::routeFactory()->routeMap();
$routeMap->get(static::class, static::ROUTE_URL, $this);
$routeMap->get(static::class . ':viewTopic', static::ROUTE_URL_VIEW_TOPIC, $this);
$routeMap->get(static::class . ':addTopic', static::ROUTE_URL_ADD_TOPIC, $this);
$routeMap->post(static::class . ':addTopicAction', static::ROUTE_URL_ADD_TOPIC_ACTION, $this);
$routeMap->allows(RequestMethodInterface::METHOD_POST);
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
Thanks for the help!
/**
* Initialization.
*
* @return void
*/
public function boot(): void
{
$routeMap = Registry::routeFactory()->routeMap();
$routeMap->get(static::class, static::ROUTE_URL, $this);
$routeMap->get(static::class . ':viewTopic', static::ROUTE_URL_VIEW_TOPIC, $this);
$routeMap->get(static::class . ':addTopic', static::ROUTE_URL_ADD_TOPIC, $this);
$routeMap->post(static::class . ':addTopicAction', static::ROUTE_URL_ADD_TOPIC_ACTION, $this);
$routeMap->allows(RequestMethodInterface::METHOD_POST);
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
Please Log in or Create an account to join the conversation.
- fisharebest
- Offline
- Administrator
2 weeks 5 days ago #6
by fisharebest
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Replied by fisharebest on topic Post request triggers 405 error with no debug message
In your boot() function, you set up something to handle the following routes:
GET: ForumModule
POST: ForumModule::addTopicAction
But, your form was sending
POST: ForumModule
The router says "Hey - you're POSTing to this URL, but it's only set up for a GET". It sends a 405.
From looking at the code, the fix is (obviously!) to update the URL used in your form. i.e. change
<form method="post" action="<?= e(route(ForumModule::class, )) ?>">
to
<form method="post" action="<?= e(route(ForumModule::class . ':addTopicAction', )) ?>">
You tell me this makes no difference. This is surprising.
My asking you to check the HTTP details in your browser's dev tools is my polite way of saying I don't believe you
Either you didn't do this change or there's something else going on. Maybe you have two forms and only changed one. I don't know.
But I want to see what the dev tools says.
When debugging things like this, you need to abandon all assumptions and look for absolute facts. What you are telling me conflicts with what you showed me.
GET: ForumModule
POST: ForumModule::addTopicAction
But, your form was sending
POST: ForumModule
The router says "Hey - you're POSTing to this URL, but it's only set up for a GET". It sends a 405.
From looking at the code, the fix is (obviously!) to update the URL used in your form. i.e. change
<form method="post" action="<?= e(route(ForumModule::class, )) ?>">
to
<form method="post" action="<?= e(route(ForumModule::class . ':addTopicAction', )) ?>">
You tell me this makes no difference. This is surprising.
My asking you to check the HTTP details in your browser's dev tools is my polite way of saying I don't believe you
Either you didn't do this change or there's something else going on. Maybe you have two forms and only changed one. I don't know.
But I want to see what the dev tools says.
When debugging things like this, you need to abandon all assumptions and look for absolute facts. What you are telling me conflicts with what you showed me.
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Please Log in or Create an account to join the conversation.
- jbldn1982
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
2 weeks 4 days ago #7
by jbldn1982
Replied by jbldn1982 on topic Post request triggers 405 error with no debug message
Hi Greg,
Thanks for the long reply and your help. Indeed I must have messed something up. I think the issue is that it is the same url but only has a different query parameter (?action=add_topic or so).
Anyhow - managed to created a blank working example of posting to a form in the new webtrees by using a different url.
Maybe it helps other developers who try to do something similar:
github.com/jbh4x82/webtrees_post_example
Thanks again, Johannes
Thanks for the long reply and your help. Indeed I must have messed something up. I think the issue is that it is the same url but only has a different query parameter (?action=add_topic or so).
Anyhow - managed to created a blank working example of posting to a form in the new webtrees by using a different url.
Maybe it helps other developers who try to do something similar:
github.com/jbh4x82/webtrees_post_example
Thanks again, Johannes
Please Log in or Create an account to join the conversation.
- jbldn1982
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
2 weeks 4 days ago #8
by jbldn1982
Replied by jbldn1982 on topic Post request triggers 405 error with no debug message
And yes - you were of course right. Found the error. Hope my example will at least help people. Thanks for helping me all the time.
Please Log in or Create an account to join the conversation.