使用Faraday gem在Ruby中发布请求并从Python翻译

时间:2015-10-30 15:02:27

标签: python ruby-on-rails ruby faraday

我试图使用法拉第宝石发布请求。

我收到信用卡信息并将其发布到网址。但是,出于某种原因,我的回答是#404; 404"。

我的问题是关于我得到回复的方式,以及我是否正确地为报头和正文提供了信息。

因为此请求没有Ruby API,只有Python,我通过从Python转换为Ruby来提出请求。

这是Ruby代码:

conn2 = Faraday.new(:url => 'https://api.iamport.kr/subscribe/payments')

  response2 = conn2.post do |req|
    req.url '/onetime'
    req.headers['X-ImpTokenHeader'] = "token secret will be put here"
    req.body = '{ "token": "abcdefg", "merchant_uid": "merchant_144609705412313",
    "amount" : "1000", "card_number" : "555555555555", "expiry": "0520", "birth": "878787", 
    "pwd_2digit": "99"}'
  end

  @body2 = response2.body
  @status2 = response2.status

Python代码:

  def onetime(self, **params):
        url = 'https://api.iamport.kr/subscribe/payments/onetime/'
        keys = ['token', 'merchant_uid', 'amount', 'vat', 'card_number', 'expiry', 'birth', 'pwd_2digit',
                'remember_me', 'customer_uid', 'buyer_name', 'buyer_email', ]
        data = {k: v for k, v in params.items() if k in keys}


    def _post(self, url, data=None, headers=None):
        data, headers = self._set_default(data, headers)
        response = requests.post(url, headers=headers, data=data)

        return self._parse_response(response)

    def _set_default(self, data, headers):
        if not data:
            data = {}

        if not headers:
            headers = {}
        headers[self.TOKEN_HEADER] = self._access_code

        return data, headers

0 个答案:

没有答案