2017-03-24 17 views
0

我想測試我的API是否工作。要做到這一點,我運行下面的代碼行:使用curl做一個api POST(調試幫助)

mftaff:~/workspace (api-controller) $ curl -v -H "Accept: application/json" -H "Origin: https://interstellar-wiki-mftaff.c9users.io" -H "Content-Type: application/json" -X POST -d '{"name":"foobar"}' https://blocmetrics-mftaff.c9users.io:8080/api/events |& tee -a output.txt

然而,當我運行此我收到以下輸出到控制檯:

* Hostname was NOT found in DNS cache 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0* Trying 104.197.98.45... 
* Connected to blocmetrics-mftaff.c9users.io (104.197.98.45) port 8080 (#0) 
* successfully set certificate verify locations: 
* CAfile: none 
    CApath: /etc/ssl/certs 
* SSLv3, TLS handshake, Client hello (1): 
} [data not shown] 
* SSLv3, TLS handshake, Server hello (2): 
{ [data not shown] 
* SSLv3, TLS handshake, CERT (11): 
{ [data not shown] 
* SSLv3, TLS handshake, Server key exchange (12): 
{ [data not shown] 
* SSLv3, TLS handshake, Server finished (14): 
{ [data not shown] 
* SSLv3, TLS handshake, Client key exchange (16): 
} [data not shown] 
* SSLv3, TLS change cipher, Client hello (1): 
} [data not shown] 
* SSLv3, TLS handshake, Finished (20): 
} [data not shown] 
* SSLv3, TLS change cipher, Client hello (1): 
{ [data not shown] 
* SSLv3, TLS handshake, Finished (20): 
{ [data not shown] 
* SSL connection using ECDHE-RSA-AES256-SHA384 
* Server certificate: 
* subject: OU=Domain Control Validated; OU=EssentialSSL Wildcard; CN=*.c9users.io 
* start date: 2016-10-17 00:00:00 GMT 
* expire date: 2017-11-01 23:59:59 GMT 
* subjectAltName: blocmetrics-mftaff.c9users.io matched 
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA 
* SSL certificate verify ok. 
> POST /api/events HTTP/1.1 
> User-Agent: curl/7.35.0 
> Host: blocmetrics-mftaff.c9users.io:8080 
> Accept: application/json 
> Origin: https://interstellar-wiki-mftaff.c9users.io 
> Content-Type: application/json 
> Content-Length: 17 
> 
} [data not shown] 
* upload completely sent off: 17 out of 17 bytes 
< HTTP/1.1 401 Unauthorized 
< x-xss-protection: 1; mode=block 
< x-content-type-options: nosniff 
< content-type: application/json; charset=utf-8 
< cache-control: no-cache 
< x-request-id: d62f648d-a901-4029-be1b-08aa65cc4b92 
< x-runtime: 0.008397 
* Server WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25) is not blacklisted 
< server: WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25) 
< date: Fri, 24 Mar 2017 08:18:39 GMT 
< content-length: 61 
< X-BACKEND: apps-proxy 
< 

21 78 0  0 100 17  0  12 0:00:01 0:00:01 --:--:-- 12{ [data not shown] 

100 78 100 61 100 17  42  11 0:00:01 0:00:01 --:--:-- 42 
* Connection #0 to host blocmetrics-mftaff.c9users.io left intact 
{"error":"You need to sign in or sign up before continuing."} 

我很新的一切這......並且我不知道如何使這個輸出的正面或反面在識別哪裏出了什麼問題。

我所知道的,到目前爲止: 有某種故障/錯誤的事情,因爲不創建一個新的事件(這是本API POST的目標)。 我已登錄主機網站和原始網站,因此有關登錄錯誤的最後一行令人困惑。

任何幫助確定錯誤可能會出現的地方將是超級!

(如果讀者覺得他需要更多的信息,請留下評論,我會很樂意提供它!)

+0

正在通過瀏覽器登錄時,只會將cookie保存在瀏覽器中。 curl完全獨立,您需要在請求中發送某種授權令牌。 –

+0

'HTTP/1.1 401 Unauthorized'意味着缺少HTTP授權,請參閱curl的'--user'和朋友。 –

回答

0

正如你可以在底部

< HTTP/1.1 401 Unauthorized 

看到和較低

{"error":"You need to sign in or sign up before continuing."} 

401部分是Web服務器向您發送狀態碼,告訴您您無權查看內容,即。您未登錄或未使用正確的http認證憑證。
正如@DanielStenberg在評論中所說的,你可以使用curl的-u來設置http auth creds。您可以輸入man curl以查看更多選項。
但是這也可能意味着您需要登錄並擁有適當的Cookie,因此您需要與正在運行應用程序的人進行溝通,並找出必要的信息。

{"error": "You need..."}部分是您從服務器獲得的實際響應主體。這是一個JSON字符串,就像您在curl調用的-H "Accept: application/json"中所要求的那樣。

所以你需要首先「登錄」。

對於較短的輸出結果,您可以更容易地閱讀,但可以省略curl命令的-v,儘管它包含了很多信息。

+0

感謝您提供這樣一個詳細的答案! – mtaff