Httparty帖子返回'422'Unprocessable Entity“'

时间:2011-03-04 15:19:25

标签: ruby httparty

我在解决这个问题上遇到了很多麻烦。这个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

1 个答案:

答案 0 :(得分:-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