1

I have a REST endpoint that I can get an access token from. To get the access token (JSON web token, JWT) and export that value as an environment variable, I do the following.

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

I then echo this token back to the console with echo $ACCESS_TOKEN and get something like this.

 eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzg5NDV9.COGBYBrx3oQvA2kIiObBOYkEFIL2BODcrSivxWvhuLs-aLsrMGO2z2aCddpwS2yZUB88Q3GOIU8QklbnfRMprQ

Note that there is a space before the first character. I didn't think that was a problem because if I exported the value directly from the console and then echoed it back out, the space is still there.

export ACCESS_TOKEN=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1MTBlMjRiYWZmZTY0NjMyOGRiNjg1N2ViMTdlZTE1NCIsImFkZHIiOiIwOjA6MDowOjA6MDowOjEiLCJzY2hlbWUiOiJodHRwIiwicG9ydCI6IjgwODAiLCJpYXQiOjE0NjgzNzg5NDV9.COGBYBrx3oQvA2kIiObBOYkEFIL2BODcrSivxWvhuLs-aLsrMGO2z2aCddpwS2yZUB88Q3GOIU8QklbnfRMprQ

Now I need to use this token to test my REST endpoints, and tried something like the following.

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

However, I get the following output.

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

If I just directly do export ACCESS_TOKEN=.... in the shell followed by the exact same curl command, then everything works.

Also if I put the export in a sh file, followed by the curl command above then it also works.

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

Any idea on what I'm doing wrong here?

  • 1
    The space is definitely a problem. The option -H consumes one argument. If the variable $ACCESS_TOKEN contains a space, then there will suddenly be two arguments, and the entire rest of the command line will end up as garbage. In order to fix the problem, you need to quote the variable, i.e. "$ACCESS_TOKEN". – Michael Vehrs Jul 13 '16 at 6:34
  • If I use double quotes, then, from debugging on the backend server, the string literal value of "$ACCESS_TOKEN" is passed in (including the double quotes). It seems there is a problem with string interpolation. – Jane Wayne Jul 13 '16 at 15:39
  • I assume you used 'x-access-token: "$ACCESS_TOKEN"'. I meant 'x-access-token: '"$ACCESS_TOKEN"''. – Michael Vehrs Jul 14 '16 at 5:17
1

The problem was with the use of -i as this option includes the headers in the output.

The strange thing is that unless you do echo "$ACCESS_TOKEN" you won't see the headers polluting the REST response coming back.

Simply remove -i and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.