下列的这例子。
当我向网站发送命令时,它会起作用并返回一个令牌:
[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 ~]$
然后我应该使用这个令牌获取GET
一些关于用户的信息。我尝试了以下方法:
[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 ~]$
但结果却是:
{"auth":false,"message":"No token provided."}
[idf@xxxx ~]$
我几乎肯定curl
错误地使用 来传递参数GET
。形成curl GET
字符串的正确方法是什么?
答案1
HTTP GET 请求参数在 URL 中传递如下:
http://example.com/path/file?argument=value
因此,对于您来说,Curl 命令行将是:
curl http://localhost:3002/api/auth/me?x-access-token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwODlhNGZkZTViNzM5N2M1YjQ5YiIsImlhdCI6MTUxNzQyMjc0NiwiZXhwIjoxNTE3NTA5MTQ2fQ.7a5KPDvB-13KYrAfkBa_2-d3XKru1J1OkxWlofnxhFw
假设 API 确实应该通过 GET 请求和查询参数来工作,那么这应该可以工作。
但是,该x-access-token
参数听起来更像是 HTTP 标头,然后您需要使用以下命令行:
curl -H "x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNzIwODlhNGZkZTViNzM5N2M1YjQ5YiIsImlhdCI6MTUxNzQyMjc0NiwiZXhwIjoxNTE3NTA5MTQ2fQ.7a5KPDvB-13KYrAfkBa_2-d3XKru1J1OkxWlofnxhFw" http://localhost:3002/api/auth/me