1

I'm trying to use the Faraday gem to post a request.

I receive credit card information and post it to a URL. But, for some reason, my response is "404".

My questions are about the way I get the response, and whether I put the information correctly for the headers and body.

Because there was no Ruby API for this request but only Python, I made a request by translating from Python to Ruby.

This is the Ruby code:

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

And the Python code:

  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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Sungpah Lee
  • 1,003
  • 1
  • 13
  • 31
  • 1
    I am pretty sure by writing `req.url '/onetime'` you effectively request `https://api.iamport.kr/onetime`. This is super easy to debug but the way, just check the response object for the details of the request – Mike Szyndel Oct 30 '15 at 16:31
  • It's now working!! Now, I got a response from it. "\uce012\ucXX\ucexx..." I think this tells me something but am not sure what it exactly tells me. Is there any translator of this type of string? ( I think this should be other question in Stackoverflow!) – Sungpah Lee Oct 30 '15 at 16:45
  • I also read something that it is also ok to pass the token information by adding "_token=?" + access_token in the url. It works! (: – Sungpah Lee Oct 30 '15 at 16:52
  • 1
    I think you can safely remove this question as this was a simple typographical error. – Mike Szyndel Oct 30 '15 at 16:55

0 Answers0