1
resposta

Node Js e HTTP - Aula 02 / Atividade 01. Problema ao fazer um comando POST no CMD com JSON junto.

Olá, tudo bem ? Estou tendo dificuldades ao fazer um comando POST no CMD, junto com um código JSON. O comando é feito pelo CURL, e suas sintaxe é a seguinte:

curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "Content-type: application/json" -d ' { "forma de pagamento": "payfast",  "valor": 10.98,  "moeda": "BRL", "descricao": "criando um pagamento"} '  

Para o JSON ficar mais fácil de visualizar, colocarei o código dele também, o mesmo está aqui: `

{
    "forma de pagamento":"payfast",
    "valor":10.98,
    "moeda":"BRL",
    "descricao":"criando um pagamento"
}

Mas o CMD mostra uma mensagem de erro de sintaxe, mas fiz exatamente como o professor do curso disse. O erro que o CMD mostra é o seguinte:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   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.61.1
> Accept: */*
> Content-type: application/json
> Content-Length: 2
>
* upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 989
< Date: Sat, 22 Sep 2018 20:02:01 GMT
< Connection: keep-alive
<
SyntaxError: Unexpected token &#39; in JSON at position 0<br> &nbsp; &nbsp;at JS
ON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError (C:\Use
rs\Everson\Documents\MURILO\payfast\node_modules\body-parser\lib\types\json.js:1
58:10)<br> &nbsp; &nbsp;at parse (C:\Users\Everson\Documents\MURILO\payfast\node
_modules\body-parser\lib\types\json.js:83:15)<br> &nbsp; &nbsp;at C:\Users\Evers
on\Documents\MURILO\payfast\node_modules\body-parser\lib\read.js:121:18<br> &nbs
p; &nbsp;at invokeCallback (C:\Users\Everson\Documents\MURILO\payfast\node_modul
es\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users\Everson\Document
s\MURILO\payfast\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at Incom
ingMessage.onEnd (C:\Users\Everson\Documents\MURILO\payfast\node_modules\raw-bod
y\index.js:273:7)<br> &nbsp; &nbsp;at emitNone (events.js:106:13)<br> &nbsp; &nb
sp;at IncomingMessage.emit (events.js:208:7)<br> &nbsp; &nbsp;at endReadableNT (
_stream_readable.js:1064:12)
* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: forma de pagamento:/
* Host name 'forma de pagamento' contains bad letter
* Closing connection -1
curl: (3) Host name 'forma de pagamento' contains bad letter
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: payfast,/
* Could not resolve host: payfast,
* Closing connection 1
curl: (6) Could not resolve host: payfast,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: valor:/
* Connection 0 seems to be dead!
* Closing connection 0
* Could not resolve host: valor
* Closing connection 2
curl: (6) Could not resolve host: valor
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: 10.98,/
* Could not resolve host: 10.98,
* Closing connection 3
curl: (6) Could not resolve host: 10.98,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: moeda:/
* Could not resolve host: moeda
* Closing connection 4
curl: (6) Could not resolve host: moeda
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: BRL,/
* Could not resolve host: BRL,
* Closing connection 5
curl: (6) Could not resolve host: BRL,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: descricao:/
* Could not resolve host: descricao
* Closing connection 6
curl: (6) Could not resolve host: descricao
curl: (3) [globbing] unmatched close brace/bracket in column 21

Quando executo o comando sem o código JSON, não aparece nenhum erro. Caso alguém saiba como resolver, agradeço. Abraços !

1 resposta

Oi Murilo, tudo bem? as chaves do seu json não podem ter espaços, então "forma de pagamento" deveria ser "forma_de_pagamento". Testa isso pra mim?