简体   繁体   中英

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

I am attempting to authenticate with a supplier's API, but I keep getting a 400 error. Mind you, it works fine with their "Mock Server"

The code I have is:

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

The response I am getting is

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

Is something wrong with my syntax here? I don't know why it is saying the email is missing.

EDIT: API Docs at https://beta.b2bapi.trevcoinc.com/#documentation/

Hard to tell where the problem lies, perhaps you are not sending the correct payload format/structure (I cannot access the link to the docs, its prompting me for authentication).

When I use RestClient I prefer the verbose format so I can see/control exactly whats happening:

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)

Try that?

It looks like you're not serializing your values hash to JSON. Rest Client unfortunately doesn't understand JSON directly, so even if you set a content-type it will encode your payload hash to a string using the application/x-www-form-urlencoded encoding.

Have you tried values.to_json instead? (This explicitly encodes to a JSON string.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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