How do I pipe the output of a curl command to an environment variable and use it in another curl command?

Jane Wayne

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?

Jane Wayne

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add a variable to this curl command?

From Dev

How do I add a variable to this curl command?

From Dev

curl command output in a variable

From Dev

Pipe output to environment variable export command

From Dev

How do I get the output of jq to pipe to curl

From Dev

How to pipe git log to curl command

From Dev

How can I extract part of the output of a curl command and assign it to a shell variable?

From Dev

How can I extract part of the output of a curl command and assign it to a shell variable - part 2?

From Dev

Trying to use Applescript variable in curl command inside do shell script

From Dev

Shell script -- how to store curl command WITH variables result into another variable?

From Dev

Use curl command output to send email

From Dev

Reduce output of curl command

From Dev

Pipe the result of a cut command to curl

From Dev

How do i do the following curl command in Java

From Dev

bash variable in curl command

From Dev

bash variable in curl command

From Dev

How do I install Composer without cURL command line?

From Dev

How I do the equivalent of this curl command using Spring's RestTemplate?

From Dev

How do I install Composer without cURL command line?

From Dev

How do I pipe a newline separated list as arguments to another command?

From Dev

How do I pipe a newline separated list as arguments to another command?

From Dev

How to set variable in the curl command in bash?

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

how to use sudo with curl command for setuptools install

From Dev

How to use the following curl post command in java?

From Dev

How to pipe a single item from a command output into another command?

From Dev

How to pipe output of command to another command while also displaying it on the console?

From Dev

How to pipe single result from output of locate command to another command?

Related Related

  1. 1

    How do I add a variable to this curl command?

  2. 2

    How do I add a variable to this curl command?

  3. 3

    curl command output in a variable

  4. 4

    Pipe output to environment variable export command

  5. 5

    How do I get the output of jq to pipe to curl

  6. 6

    How to pipe git log to curl command

  7. 7

    How can I extract part of the output of a curl command and assign it to a shell variable?

  8. 8

    How can I extract part of the output of a curl command and assign it to a shell variable - part 2?

  9. 9

    Trying to use Applescript variable in curl command inside do shell script

  10. 10

    Shell script -- how to store curl command WITH variables result into another variable?

  11. 11

    Use curl command output to send email

  12. 12

    Reduce output of curl command

  13. 13

    Pipe the result of a cut command to curl

  14. 14

    How do i do the following curl command in Java

  15. 15

    bash variable in curl command

  16. 16

    bash variable in curl command

  17. 17

    How do I install Composer without cURL command line?

  18. 18

    How I do the equivalent of this curl command using Spring's RestTemplate?

  19. 19

    How do I install Composer without cURL command line?

  20. 20

    How do I pipe a newline separated list as arguments to another command?

  21. 21

    How do I pipe a newline separated list as arguments to another command?

  22. 22

    How to set variable in the curl command in bash?

  23. 23

    How To Use A Command Output As An Argument of Another Command?

  24. 24

    How To Use A Command Output As An Argument of Another Command?

  25. 25

    how to use sudo with curl command for setuptools install

  26. 26

    How to use the following curl post command in java?

  27. 27

    How to pipe a single item from a command output into another command?

  28. 28

    How to pipe output of command to another command while also displaying it on the console?

  29. 29

    How to pipe single result from output of locate command to another command?

HotTag

Archive