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

Erro ao executar o POST via Curl

O retorno da minha requisição dá 400, Bad Request, aí estão o comando e sua respectiva resposta. Quando tento colar o json do arquivo ele abre o arquivo no bloco de notas. O que estou fazendo de errado?

C:\Users\Pantufa>curl http://localhost:3000/pagamentos/pagamento -X POST -v -H "
Content-type: application/json" -d '{"forma_de_pagamento":"payfast","valor":10.9
8,"moeda":"BRL","descricao":"criando um pagamento"}'
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.63.0
> Accept: */*
> Content-type: application/json
> Content-Length: 83
>
* upload completely sent off: 83 out of 83 bytes
< HTTP/1.1 400 Bad Request
< X-Powered-By: Express
< Content-Security-Policy: default-src 'self'
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< Content-Length: 1046
< Date: Sun, 23 Dec 2018 20:47:20 GMT
< Connection: keep-alive
<
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token &#39; in JSON at position 0<br> &nbsp; &nbsp;
at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at createStrictSyntaxError (C
:\Users\Pantufa\payfast\node_modules\body-parser\lib\types\json.js:158:10)<br> &
nbsp; &nbsp;at parse (C:\Users\Pantufa\payfast\node_modules\body-parser\lib\type
s\json.js:83:15)<br> &nbsp; &nbsp;at C:\Users\Pantufa\payfast\node_modules\body-
parser\lib\read.js:121:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\Pantufa\p
ayfast\node_modules\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users
\Pantufa\payfast\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at Incom
ingMessage.onEnd (C:\Users\Pantufa\payfast\node_modules\raw-body\index.js:273:7)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:182:13)<br> &nbsp; &nbsp;at
 endReadableNT (_stream_readable.js:1094:12)<br> &nbsp; &nbsp;at process._tickCa
llback (internal/process/next_tick.js:63:19)</pre>
</body>
</html>
* Connection #0 to host localhost left intact
3 respostas

Oi Clarisse

Pelo que eu vi aqui pode ser uma confusão com as aspas, tenta dessa maneira

    curl http://localhost:3000/pagamentos/pagamento \
    -X POST \ 
    -v \
    -H "Content-type: application/json" \
    -d '{
            "forma_de_pagamento": "payfast",
            "valor": "10.87",
            "moeda": "BRL",
            "descricao": "descrição do pagamento"
        }'
solução!

Tentei e deu o mesmo erro. Lendo os exercícios, eu tentei o comando

curl http://localhost:3000/pagamentos/pagamento -X POST -v  -H "Content-type: application/json" -d @pagamento.json

que funcionou. Realmente no momento em que coloco as aspas simples no sistema ele fica esquisito.

Legal Clarisse,

Marca sua resposta como solução, se mais algum aluno tiver dúvida já vão ter uma resposta