sh13
November 7, 2017, 2:30pm
1
I’m trying to update my DB using PUT method, but I can’t do it and I don’t know why. I’m using Postman to test my api.
Can anybody help me?
This is my code
$app->put('/usuario/[{correo}]', function ($request, $response, $args) {
$input = $request->getParsedBody();
$sql = "UPDATE usuarios SET usuario=:nombre, password=:clave WHERE email=:correo";
$sth = $this->db->prepare($sql);
$sth->bindParam("nombre", $input['nombre']);
$sth->bindParam("correo", $args['correo']);
$sth->bindParam("clave", $input['clave']);
$sth->execute();
$input['correo'] = $args['correo'];
return $this->response->withJson($input);
` });
This is my DB
Postman view
Which Headers are you sending?
sh13
November 7, 2017, 3:20pm
4
You can see it in the image.
Content-Type: application/x-www-form-urlencoded
Accept: application/json
This app is working fine for me…
<?php
require __DIR__ . '/../vendor/autoload.php';
$app = new \Slim\App();
$app->put('/usuario/[{correo}]', function ($request, $response, $args) {
$input = $request->getParsedBody();
$input[‘correo’] = $args[‘correo’];
return $this->response->withJson($input);
});
$app->run();
The request:
PUT /usuario/aa@aa.es HTTP/1.1
Host: slim-skeleton.localhost
Accept: application/json
User-Agent: Paw/3.1.5 (Macintosh; OS X/10.13.1) NSURLConnection/1443.14
Content-Type: application/x-www-form-urlencoded; charset=utf-8
nombre=aaa&clave=aaa
The response:
HTTP/1.1 200 OK
Server: Apache
Content-Type: application/json;charset=utf-8
X-Powered-By: PHP/7.0.14
Date: Tue, 07 Nov 2017 15:26:17 GMT
X-Frame-Options: SAMEORIGIN
Keep-Alive: timeout=5, max=100
Content-Length: 56
Connection: Keep-Alive
{"nombre":"aaa","clave":"aaa","\u2018correo\u2019":null}
I was thinking that the issue might be something in your database code, but since you are getting a 404
response I suspect something is wrong with your webserver configuration. It doesn’t look like you are hitting the route.
sh13
November 7, 2017, 3:36pm
6
I use other methods (GET, POST) and I have no problems, just with this one.
Try removing the “Content-Type: application/x-www-form-urlencoded”, but from where the message is printed?
Are you sure the right route is hit?
The 404
suggests you are not hitting that route.
sh13
November 7, 2017, 4:35pm
9
I admit that you are right, guys, but I can’t understand it because the route seems correct.
$app->put('/usuario/[{correo}]', function ($request, $response, $args) { ... }
tflight
November 7, 2017, 4:59pm
10
Can you try it from a client other than Postman to eliminate that variable such as Paw or curl? I know I have difficulty using Paw’s default HTTP engine when connecting to localhost on a non-default port but if I use the OS default it works fine.
## Request
curl -X "PUT" "http://localhost:8080/usuario/aaa@aaa.es" \
-H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
-H 'Accept: application/json' \
--data-urlencode "clave=aaa" \
--data-urlencode "nombre=aaa"
Do you hit the route (check the response status code) with curl (above)? Or Guzzle (below)?
<?php
// Include Guzzle. If using Composer:
// require 'vendor/autoload.php';
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$request = new Request(
"PUT",
"http://localhost:8080/usuario/aaa@aaa.es",
[
"Content-Type" => "application/x-www-form-urlencoded; charset=utf-8",
"Accept" => "application/json"
],
"nombre=aaa&clave=aaa");
$response = $client->send($request);
echo "Response HTTP : " . $response->getStatusCode() . "
";
sh13
November 7, 2017, 5:47pm
11
Thanks, guys, I’ve solved it changing the route.
$app->put('/usuario', function ($request, $response, $args) {
$input = $request->getParsedBody();
$sql = "UPDATE usuarios SET usuario=:nombre, password=:clave WHERE email=:correo";
$sth = $this->db->prepare($sql);
$sth->bindParam("nombre", $input['nombre']);
$sth->bindParam("correo", $input['correo']);
$sth->bindParam("clave", $input['clave']);
$sth->execute();
return $this->response->withJson($input);
});