0

我很难弄清楚这一点。此 curl 命令工作正常:

curl -v -k --basic -u"username:password" -XPOST -H"X-API-VERSION:1" -H"Accept:application/json" -H"Content-Type:application/x-www-form-urlencoded" -data'jsonData={"email":"tech@usamp.com"}' https://api.somenetwork.net/coreg/users

但是,httparty 不断返回 '422 "Unprocessable Entity" '。

这是我的代码:

class CoregBase
  include HTTParty

  def initialize
    self.class.base_uri "https://api.somenetwork.net/coreg"
    self.class.basic_auth "username", "password"
    self.class.headers({'X-API-VERSION' => '1',
       'Accept' => 'application/json'})
  end
end 


class Users < CoregBase
  include HTTParty

  def initialize
    super
  end

  def create_co_registration_user
    self.class.headers({ 'X-API-VERSION' => '1',
          'Accept' => 'application/json',
          'Content-Type' => "application/x-www-form-urlencoded"})

    options = {
    :body => {"email" => "dms_testABCC@test.com"}} 

    self.class.post('/users', options)
  end
end

coreg_user = Users.new
result = coreg_user.create_co_registration_user
pp result
4

1 回答 1

-1

好吧,没关系,我想通了:

def create_co_registration_user
  self.class.headers({ 'X-API-VERSION' => '1',
      'Accept' => 'application/json',
      'Content-Type' => "application/x-www-form-urlencoded"})

  j_data = {"email" => "dms_testABCC@test.com"}.to_json

  options = {:body => "jsonData=#{j_data}"}

  self.class.post('/users', options)
end
于 2011-03-04T21:02:33.240 回答