0

Following this example.

When I send a command to the website as thus, it works and returns a token:

[idf@xxxx ~]$  curl -d "name=Ivan&email=idf@xxxcom&password=secret" -H "Content-Type: application/x-www-form-urlencoded" -X POST localhost:3002/api/auth/register 

{"auth":true,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwYzZkYzI2ZjNlNzRhMjZhODg1YiIsImlhdCI6MTUxNzQyMzcyNSwiZXhwIjoxNTE3NTEwMTI1fQ.W_lffqIlGh21UD2o4Sqd6mISrAC6PKv6swTQvQV1IfU"}

[idf@xxxx ~]$ 

I am then supposed to use this token to GET some information about the user. I tried this:

[idf@xxx ~]$  curl -v -d "x-access-token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwODlhNGZkZTViNzM5N2M1YjQ5YiIsImlhdCI6MTUxNzQyMjc0NiwiZXhwIjoxNTE3NTA5MTQ2fQ.7a5KPDvB-13KYrAfkBa_2-d3XKru1J1OkxWlofnxhFw" -H "Content-Type: application/x-www-form-urlencoded" -X GET localhost:3002/api/auth/me
* About to connect() to localhost port 3002 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 3002 (#0)
> GET /api/auth/me HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:3002
> Accept: */*
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 186
> 
* upload completely sent off: 186 out of 186 bytes
< HTTP/1.1 403 Forbidden
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 45
< ETag: W/"2d-vj/ohbytTg+R1uXxU34fCnv4o0o"
< Date: Wed, 31 Jan 2018 18:29:45 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
[idf@xxxx ~]$ 

But it comes back with:

{"auth":false,"message":"No token provided."}

[idf@xxxx ~]$

I am almost certainly using curl incorrectly to pass a parameter to GET. What is the correct way to form the curl GET string?

1 Answer 1

2

HTTP GET request arguments are passed in the URL like this:

http://example.com/path/file?argument=value

So, in your case, the Curl command line would be:

curl http://localhost:3002/api/auth/me?x-access-token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwODlhNGZkZTViNzM5N2M1YjQ5YiIsImlhdCI6MTUxNzQyMjc0NiwiZXhwIjoxNTE3NTA5MTQ2fQ.7a5KPDvB-13KYrAfkBa_2-d3XKru1J1OkxWlofnxhFw

This should work assuming that the API is really supposed to work via GET requests and query arguments.

However, the x-access-token argument sounds more like an HTTP header, and then you would need to use this command line:

curl -H "x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwODlhNGZkZTViNzM5N2M1YjQ5YiIsImlhdCI6MTUxNzQyMjc0NiwiZXhwIjoxNTE3NTA5MTQ2fQ.7a5KPDvB-13KYrAfkBa_2-d3XKru1J1OkxWlofnxhFw" http://localhost:3002/api/auth/me
1
  • I'm curious, which one works? Feb 1, 2018 at 23:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .