Ruby on rails 使用curl生成api POST(调试帮助)

Ruby on rails 使用curl生成api POST(调试帮助),ruby-on-rails,debugging,curl,Ruby On Rails,Debugging,Curl,我正在尝试测试我的API是否正常工作。为此,我运行以下代码行: mftaff:~/workspace(api控制器)$curl-v-H“接受:应用程序/json”-H”来源:https://interstellar-wiki-mftaff.c9users.io“-H”内容类型:application/json“-X POST-d'{“name”:“foobar”}”https://blocmetrics-mftaff.c9users.io:8080/api/events |&tee-a outp

我正在尝试测试我的API是否正常工作。为此,我运行以下代码行:

mftaff:~/workspace(api控制器)$curl-v-H“接受:应用程序/json”-H”来源:https://interstellar-wiki-mftaff.c9users.io“-H”内容类型: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."}
在DNS缓存中找不到主机名 %总接收百分比%x平均速度时间电流 数据加载上载总左速度 0 0 0 0 0 0 0--:-:-:-:-:-:---:---0*正在尝试104.197.98.45。。。 *连接到blocmetrics-mftaff.c9users.io(104.197.98.45)端口8080(#0) *已成功设置证书验证位置: *卡菲尔:没有 CApath:/etc/ssl/certs *SSLv3,TLS握手,客户端hello(1): }[未显示数据] *SSLv3,TLS握手,服务器hello(2): {[未显示数据] *SSLv3,TLS握手,证书(11): {[未显示数据] *SSLv3、TLS握手、服务器密钥交换(12): {[未显示数据] *SSLv3,TLS握手,服务器完成(14): {[未显示数据] *SSLv3、TLS握手、客户端密钥交换(16): }[未显示数据] *SSLv3,TLS更改密码,客户端你好(1): }[未显示数据] *SSLv3,TLS握手,完成(20): }[未显示数据] *SSLv3,TLS更改密码,客户端你好(1): {[未显示数据] *SSLv3,TLS握手,完成(20): {[未显示数据] *使用ECDHE-RSA-AES256-SHA384的SSL连接 *服务器证书: *主题:OU=已验证的域控制;OU=基本LSSL通配符;CN=*.c9users.io *开始日期:2016-10-17 00:00:00 GMT *截止日期:2017-11-01 23:59:59 GMT *subjectAltName:blocmetrics-mftaff.c9users.io匹配 *发卡机构:C=GB;ST=Greater Manchester;L=Salford;O=COMODO CA Limited;CN=COMODO RSA域验证安全服务器CA *SSL证书验证正常。 >POST/api/events HTTP/1.1 >用户代理:curl/7.35.0 >主机:blocmetrics mftaff.c9users.io:8080 >接受:application/json >来源:https://interstellar-wiki-mftaff.c9users.io >内容类型:application/json >内容长度:17 > }[未显示数据] *完全发送上载:17个字节中的17个 我对这一切都很陌生……我不知道如何在识别哪里/哪里出了问题方面对这一输出做出正面或反面的判断

到目前为止我所知道的: 由于未创建新事件(这是本API文章的目标),因此出现了某种类型的故障/错误。 我已登录到主机网站和源网站,因此关于登录错误的最后一行令人困惑

识别错误可能存在的位置的任何帮助都将是极好的


(如果读者觉得需要更多信息,请留下评论,我很乐意提供!)

如下图所示

< HTTP/1.1 401 Unauthorized
401部分是向您发送状态代码的web服务器,告知您无权查看内容,即您未登录或未使用正确的http身份验证凭据。
正如@DanielStenberg在评论中所说,您可以使用curl的
-u
设置http身份验证凭据。您可以键入
man curl
查看更多选项。
但这也可能意味着你需要登录并拥有适当的cookies,因此你需要与运行该应用程序的人交谈,并找出必要的内容

{“error”:“youneed…”
部分是您从服务器获得的实际响应正文。这是一个JSON字符串,类似于您在curl调用的
-H“Accept:application/JSON”
中要求的字符串

所以你需要先“登录”


要获得更易于阅读的简短输出,您可以省略curl命令的
-v
,尽管它包含大量信息。

通过浏览器登录只会将cookie保留在浏览器中。curl是完全独立的,您需要在请求中发送某种授权令牌。
HTTP/1.1 401未经授权
意味着缺乏HTTP授权,请参阅curl的
--user
和friends。感谢您提供如此详细的答案!
{"error":"You need to sign in or sign up before continuing."}