使用HTTP gem(或RestClient)进行数组的API POST [英] API POST with array using HTTP gem (or RestClient)

查看:241
本文介绍了使用HTTP gem(或RestClient)进行数组的API POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用此api时遇到问题,似乎无法克服困难.使用 HTTP gem (尽管我很灵活,可以使用 PrintAura API

I'm having trouble with this api and can't seem to get over the hump. Using the HTTP gem (though I'm flexible and can use RestClient if that gets me an answer quicker!). Anyway, I'm having trouble posting an array. everything else is good, I just can't figure out this "itemsarray" in the printaura api found here in the addorder method: PrintAura API

我正在运行:

def self.submitorder   
  req = HTTP.post("https://api.printaura.com/api.php", :json => { 
        :key => APIKEY, 
        :hash => APIHASH, 
        :method => "addorder",
        :businessname => "this is a secret too",
        :businesscontact => "thats a secret",
        :email => "my@email.com",
        :your_order_id => "1",
        :returnlabel => "FakeAddress",
        :clientname => "ShippingName",
        :address1 => "ShippingAddressLine1",
        :address2 => "ShippingAddressLine2",
        :city => "ShippingCity",
        :state => "ShippingState",
        :zip => "ShippingZip",
        :country => "US",
        :customerphone => "dontcallme",
        :shipping_id => "1",
        :itemsarray => {:item => [ 
            :product_id => 423,
            :brand_id => 33,
            :color_id => 498,
            :size_id => 4,
            :front_print => 1389517,
            :front_mockup => 1390615,
            :quantity => 1
          ]}

    })

  puts JSON.parse(req)




end

我的输出是这样

{"status"=>false, "error_code"=>19, "result"=>19, "message"=>"You cannot place an order without items, Please fill the items array with all the required information. Full API documentation can be found at https:/www.printaura.com/api/"}

天哪,如果有人可以看看并帮助我,我将永远感激不已.

Gosh, if someone could look at that and help me out I would forever appreciate it.

推荐答案

def self.submitorder 
  itemsarray = { :items => [ { :product_id => 423, :brand_id => 33, :color_id => 498, :size_id => 4, :quantity => 1, :front_print => 1389517,
        :front_mockup => 1390617 } ] }
  req = HTTP.post("https://api.printaura.com/api.php", :json => { 
    :key => APIKEY, 
    :hash => APIHASH, 
    :method => "addorder",
    :businessname => "this is a secret too",
    :businesscontact => "thats a secret",
    :email => "my@email.com",
    :your_order_id => "1",
    :returnlabel => "FakeAddress",
    :clientname => "ShippingName",
    :address1 => "ShippingAddressLine1",
    :address2 => "ShippingAddressLine2",
    :city => "ShippingCity",
    :state => "ShippingState",
    :zip => "ShippingZip",
    :country => "US",
    :customerphone => "dontcallme",
    :shipping_id => "1",
    :items =>  Base64.encode64(itemsarray.to_json)}
)

  puts JSON.parse(req)

我真的希望这会在几年后对某人有所帮助

I really hopes this helps somebody some years from now haha

这篇关于使用HTTP gem(或RestClient)进行数组的API POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆