繁体   English   中英

使用Rest Client的API调用Ruby on Rails App的400错误

[英]400 Error for API call Ruby on Rails App using Rest Client

我正在尝试使用供应商的API进行身份验证,但始终收到400错误。 请注意,其“模拟服务器”可以正常工作

我的代码是:

def perform(*args)
    require 'rest_client'

    values = {"email": "not_telling@hotmail.com",
      "password": ENV['PASSWORD']
    }

    headers = {
      :content_type => 'application/json',
      :accept => 'application/json'
    }

    body = begin
      RestClient.post 'https://beta.b2bapi.trevcoinc.com/api/v1/login', values, headers
      rescue => e
      e.response.body
    end
    fb_response = JSON.parse(body)

  end

我得到的回应是

{"error"=>"Email is missing!", "status"=>"400"} 

我的语法有问题吗? 我不知道为什么这说电子邮件丢失了。

编辑: https ://beta.b2bapi.trevcoinc.com/#documentation/上的API文档

很难说出问题出在哪里,也许您未发送正确的有效负载格式/结构(我无法访问文档的链接,提示我进行身份验证)。

当我使用RestClient时,我更喜欢冗长的格式,因此我可以准确地看到/控制正在发生的事情:

headers = {
  'Content-Type' => 'application/json'
}
url = "https://api.vimeo.com/me/videos"
body = {
  "some" => "content"
}
res = RestClient::Request.execute(method: :post, url: url, headers: headers, payload: body.to_json)

试试吧

似乎您没有将values哈希序列化为JSON。 遗憾的是,Rest Client无法直接理解JSON,因此即使您设置了内容类型,它也会使用application/x-www-form-urlencoded编码将有效负载哈希编码为字符串。

您是否尝试过values.to_json (这明确编码为JSON字符串。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM