Httparty 邮寄退货 '第422章“不可处理的实体” '

发布于 2024-10-20 05:09:17 字数 1365 浏览 2 评论 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":"[email protected]"}' https://api.somenetwork.net/coreg/users

但是,httparty不断返回'422“无法处理的实体”'。

这是我的代码:

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" => "[email protected]"}} 

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

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

I'm having a lot of trouble figuring this out. This curl command works fine:

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":"[email protected]"}' https://api.somenetwork.net/coreg/users

However, httparty keeps returning '422 "Unprocessable Entity" '.

Here's my code:

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" => "[email protected]"}} 

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-10-27 05:09:17

好吧,没关系,我明白了:

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" => "[email protected]"}.to_json

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

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

Alright nevermind, I figured it out:

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" => "[email protected]"}.to_json

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

  self.class.post('/users', options)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文