Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

Power Shell não funciona o POST

Olá, não consigo fazer funcionar no Windows, uso o PowerShell e também o Cmder e nenhum dos dois funciona. Estou seguindo passo a passo do curso e nada.

Erro 1 - não reconhece o json_pp (Cmder)

curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "Content-type: application/json" -d @files/pagamento.json | json_pp
'json_pp' não é reconhecido como um comando interno
ou externo, um programa operável ou um arquivo em lotes.

Erro 2 - Sem o json_pp da erro 500. (Cmder)

 curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "Content-type: application/json" -d @files/pagamento.json
Note: Unnecessary use of -X or --request, POST is already inferred.
* timeout on name lookup is not supported
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /pagamentos/pagamento HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.1
> Accept: */*
> Content-type: application/json
> Content-Length: 145
>
* upload completely sent off: 145 out of 145 bytes
< HTTP/1.1 500 Internal Server Error
< X-Powered-By: Express
< Content-Security-Policy: default-src 'self'
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 1158
< Date: Tue, 01 Aug 2017 16:22:12 GMT
< Connection: keep-alive
<
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: app.persistencia.PagamentoDao is not a function<br> &nbsp; &nbsp;at C:\node\cartola\controllers\pagamentos.js:15:24<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\node\cartola\node_modules\express\lib\router\layer.js:95:5)<br> &nbsp; &nbsp;at next (C:\node\cartola\node_modules\express\lib\router\route.js:137:13)<br> &nbsp; &nbsp;at Route.dispatch (C:\node\cartola\node_modules\express\lib\router\route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\node\cartola\node_modules\express\lib\router\layer.js:95:5)<br> &nbsp; &nbsp;at C:\node\cartola\node_modules\express\lib\router\index.js:281:22<br> &nbsp; &nbsp;at Function.process_params (C:\node\cartola\node_modules\express\lib\router\index.js:335:12)<br> &nbsp; &nbsp;at next (C:\node\cartola\node_modules\express\lib\router\index.js:275:10)<br> &nbsp; &nbsp;at C:\node\cartola\node_modules\body-parser\lib\read.js:130:5<br> &nbsp; &nbsp;at invokeCallback (C:\node\cartola\node_modules\body-parser\node_modules\raw-body\index.js:262:16)</pre>
</body>
</html>
* Connection #0 to host localhost left intact

Erro com PowerShell

curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "Content-type: application/json" -d @files/pagamento.json | json_pp
json_pp : The term 'json_pp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:123
+ ... H "Content-type: application/json" -d @files/pagamento.json | json_pp
+                                                                   ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (json_pp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
4 respostas

Douglas, tudo bem ?

Tenta usar o Postman ou Advanced REST client:

Ambos são muito bons

Estou tentando usar e ele me retorna isso:

{
    "------WebKitFormBoundary0mWlrpO5xA4AmXKV\r\nContent-Disposition: form-data; name": "\"\"; filename=\"pagamento.json\"\r\nContent-Type: application/octet-stream\r\n\r\n{\r\n          \"forma_de_pagamento\": \"payfast\",\r\n          \"valor\": \"10.87\",\r\n          \"moeda\": \"BRL\",\r\n          \"descricao\": \"descrição do pagamento\"\r\n}\r\n------WebKitFormBoundary0mWlrpO5xA4AmXKV--\r\n",
    "status": "CRIADO",
    "data": "2017-08-01T17:33:32.489Z"
}

E também aparece que pagamento foi criado, porém não aparece nada no banco.

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
consign v0.1.6 Initialized in C:\node\cartola
+ .\controllers\pagamentos.js
+ .\controllers\teste.js
+ .\persistencia\connectionFactory.js
+ .\persistencia\PagamentoDao.js
Servidor rodando na porta 3000
processando pagamento...
pagamento criado

Consulto no banco e não aparece nada.

mysql> select * from pagamentos;
Empty set (0.00 sec)

connectionFactory.js

var mysql  = require('mysql');

  function createDBConnection(){
    return mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: 'root',
      database: 'payfast'
    });
  }

  module.exports = function() {
    return createDBConnection;
  }
solução!

Consegui resolver, o Postman que estou utilizando não funciona inserindo dados por arquivo (Body -> form-data -> upload de arquivo .json), só consegui simular através do (Body -> raw -> colando o codigo abaixo:)

{
          "forma_de_pagamento": "payfast",
          "valor": "10.87",
          "moeda": "BRL",
          "descricao": "descrição do pagamento"
}

Desta forma atualizou o bando:

mysql> select * from pagamentos;
+----+--------------------+-------+-------+--------+------------+------------------------+
| id | forma_de_pagamento | valor | moeda | status | data       | descricao              |
+----+--------------------+-------+-------+--------+------------+------------------------+
|  2 | payfast            | 10.87 | BRL   | CRIADO | 2017-08-01 | descrição do pagamento |
+----+--------------------+-------+-------+--------+------------+------------------------+
1 row in set (0.00 sec)

Mas é complicado não funcionar pelo terminal Windows. Vocês poderiam ensinar como faz no PowerShell ou CMD.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software