Como canalizar a saída de um comando curl para uma variável de ambiente e usá-la em outro comando curl?

0

Eu tenho um endpoint REST do qual posso obter um token de acesso. Para obter o token de acesso (JSON web token, JWT) e exportar esse valor como uma variável de ambiente, eu faço o seguinte.

export ACCESS_TOKEN=$(curl -i -H 'Content-Type: application/json' -X POST -d @credentials.json http://localhost:8080/api/user/login)

Eu então faço eco desse token de volta ao console com echo $ACCESS_TOKEN e obtenho algo assim.

 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzg5NDV9.COGBYBrx3oQvA2kIiObBOYkEFIL2BODcrSivxWvhuLs-aLsrMGO2z2aCddpwS2yZUB88Q3GOIU8QklbnfRMprQ

Observe que há um espaço antes do primeiro caractere. Eu não achei que isso fosse um problema, porque se eu exportasse o valor diretamente do console e, em seguida, fizesse um eco, o espaço ainda está lá.

export ACCESS_TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzg5NDV9.COGBYBrx3oQvA2kIiObBOYkEFIL2BODcrSivxWvhuLs-aLsrMGO2z2aCddpwS2yZUB88Q3GOIU8QklbnfRMprQ

Agora preciso usar esse token para testar meus pontos de extremidade REST e tentei algo como o seguinte.

curl -i \
 -H 'x-access-token: '$ACCESS_TOKEN'' \
 -X POST -d @mydata.json \
 http://localhost:8080/api/data

No entanto, recebo a seguinte saída.

curl: (7) Couldn't connect to server
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Server
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Access-Control-Allow-Methods
curl: (6) Could not resolve host: POST,
curl: (6) Could not resolve host: PUT,
curl: (6) Could not resolve host: GET,
curl: (6) Could not resolve host: OPTIONS,
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Access-Control-Max-Age
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Access-Control-Allow-Headers
curl: (6) Could not resolve host: Origin,
curl: (6) Could not resolve host: X-Requested-With,
curl: (6) Could not resolve host: Content-Type,
curl: (6) Could not resolve host: Accept,
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Access-Control-Allow-Credentials
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Content-Type
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Content-Length
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: Date
curl: (6) Could not resolve host: Wed,
curl: (7) Could not resolve host: Wed,
curl: (6) Could not resolve host: Jul
curl: (7) Could not resolve host: Jul
curl: (6) Could not resolve host: 03:02
curl: (3) Illegal characters found in URL
curl: (3) Illegal characters found in URL
curl: (6) Could not resolve host: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzg5NDV9.COGBYBrx3oQvA2kIiObBOYkEFIL2BODcrSivxWvhuLs-aLsrMGO2z2aCd
HTTP/1.1 100 Continue

HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-access-token
Access-Control-Allow-Credentials: true
Content-Length: 0
Date: Wed, 13 Jul 2016 03:23:04 GMT
Connection: close

Se eu fizer diretamente export ACCESS_TOKEN=.... no shell seguido pelo mesmo comando curl , então tudo funciona.

Além disso, se eu colocar a exportação em um arquivo sh , seguido pelo comando curl acima, isso também funcionará.

#!/bin/bash
export ACCESS_TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzkwMjB9.lV6jSf9w5_AbsPrNcWcgQpS-DWQVxnH65u06BDGIyL-ST_gg4xXZ2KLAs-kbwckRB3OFy637G1op6PZ2tpHdUQ

Alguma ideia do que estou fazendo errado aqui?

    
por Jane Wayne 13.07.2016 / 05:31

1 resposta

1

O problema foi com o uso de -i , pois essa opção inclui os cabeçalhos na saída.

O mais estranho é que, a menos que você faça echo "$ACCESS_TOKEN" , você não verá os cabeçalhos poluindo a resposta REST voltando.

Basta remover -i e isso deve funcionar.

    
por 13.07.2016 / 18:00