#HTTP POST request problem to update route

41 messages · Page 1 of 1 (latest)

serene nebula
#

Bonjour, j'aimerai savoir si quelqu'un peut m'aider sur une requete HTTP POST vers une route laravel. Cette route resemble a:
Route::POST('/update/{'code'}, function($code, Request request){
$objet = Objet::find(1);
$objet->code = $code;
$objet->save();
}
Plutot simple mais je n'arrive pas a faire de test avec postman par exemple

#

Problème de requete HTTP POST vers route laravel

stiff prairie
#

English?

serene nebula
#

Sorry,
I need help with a HTTP POST request. This route must recieve a Key with value to modify an entry into my db.
I'm alleady able to make a get and recieve what i want so, i'm pretty sure my setting are correct but i can't update my entry with software like postman.

narrow girder
#

The single quotes in {'code'} looks odd, and Objet:: is spelled incorrectly. Is this the problem? If not, show us the actual code.

#

Or OK, I guess Objet is your model.
But there are also missing $, I guess you re-typed the code?

#

Do you get an error?

serene nebula
#

Route::POST('/update/{'code'}, function($code, Request request){
$code = Code::find(1);
$code->code = $code;
$code->save();
}

narrow girder
#

Is that the actual code? It has syntax errors

#

You should get error messages if that is the case

serene nebula
#

I have 404 error when i try to post request

#

what should i modify?

#

Actual code Controller:

<?php

namespace App\Http\Controllers;

use App\Models\Code;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CodeController extends Controller
{
    public function update($newCode, Request $request)
    {
        $code = Code::findOrFail(1);
        $code->code_recu = $code->code_recu + $request->input($newCode);
        $code->save();
        return response()->json(['message' => 'Code updated successfully']);
    }
}

Actual web.php:

<?php

use App\Models\Code;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CodeController;

Route::get('/', function () {
    $codes = Code::all();
    return response()->json($codes);

});

Route::post('/update/{new_code}', [CodeController::class, 'update'])->name('edit');

Actual Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Code extends Model
{
    use HasFactory;

    protected $fillable =[
        'code',
        'code_recu',
        'reset'
    ];
}
#

HTTP POST request problem to update route

narrow girder
#

And there is a Code model with id 1?

serene nebula
#

actually yes

narrow girder
#

The 404 page doesn't look like it's generated by Laravel, but by Apache. Are other routes working?

serene nebula
#

GET route is working well

#

log in apache recieve GET and POST but post return 404 (actually normal because there is nothing to return ) it just have to update DB

#

but nothing happen in DB

narrow girder
#

Check the order of parameters, If your controller method is also expecting input from a route parameter, list your route arguments after your other dependencies.

#

404 means route or model not found, it does not mean "nothing to return"

serene nebula
#

ah ok ^^'

#

what do you mean by listing my argument after other dependencies?

narrow girder
#

update(Request $request, $newCode)

#

I don't see that causing 404, but the docs is clear

#

Check that you haven't cached routes, php artisan route:clear

serene nebula
#

actually a made a controller but this is really basic so i can put the function into the web.php

#

controller don't need any parameter from Request object because the parameter is into the url

#

i clear the route cache and invert the argument... still 404

bold rune
bold rune
narrow girder
bold rune
serene nebula
#

ok then, it was a problem into the apache2 configuration that never root to the laravel route

#

but now a have a419 problem -_-' i want to disable CSRF token (app in domestic network)

#

Thanks for all, problem was: in the apache configuration, i forget to configure

  <VirtualHost *:80>
    DocumentRoot /var/www/nom_de_votre_projet/public
    ServerName nom_de_votre_projet
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/nom_de_votre_projet/public> #from here
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory> #to here
</VirtualHost>
#

and disable CSRF token, due to private network this is not really usefull and avoid some problem like code 419

rotund heart
#

Rien à voir mais tu devrais utiliser des noms de variables en anglais pour éviter toute confusion 👍