开发者

How to consume Rest API with complex objects using Httparty and Ruby

开发者 https://www.devze.com 2023-01-13 04:21 出处:网络
I am trying to implement call this API method in Ruby https://zferral.com/api-docs/affiliate#aff-create-usage-example which expects this object:

I am trying to implement call this API method in Ruby https://zferral.com/api-docs/affiliate#aff-create-usage-example which expects this object:

{  "affiliate" : {
    "email"           : "john@example.com",
    "send_use开发者_如何学编程r_email" : "1" }}

Using a Httparty class, I call this:

result = self.class.post("/api/#{@apikey}/affiliate/create.xml", :body => {:affiliate => {:email => email}})

Unfortunately, the API keeps sending me "Email is required". I have tried to switch to json, I am changed :body with :query, etc...

Anybody could show me how to call the affiliate/create method correctly?

Thank you.


You're sending json, but posting it to create.xml --- in rails, this will auto-set the content type to be xml when you post to create.xml

Try posting to /api/#{@apikey}/affiliate/create.json

If that doesn't work --- are you following the class design that HTTParty really likes? http://railstips.org/blog/archives/2008/07/29/it-s-an-httparty-and-everyone-is-invited/


This is how I fixed my issue:

    url = URI.parse('https://xxxx.zferral.com/api/xxx/affiliate/create.xml')
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new(url.path)
    request.body = "<?xml version='1.0' encoding='UTF-8'?><affiliate><email>#{email}</email><send_user_email>0</send_user_email></affiliate>"
    request.content_type = 'text/xml'
    response = http.request(request)
0

精彩评论

暂无评论...
验证码 换一张
取 消