Invalid HTTP request error

I’m trying to write a module to communicate with Stripe’s payment API. Already, i’ve come across trouble. I keep getting a response: "“Your client sent an invalid HTTP request.” I’ve tried every possible thing I could think of…even b64 encryption…however, nothing seems to work. Any ideas?

Here is my code: 

description = "test" email = "test@email.com" api\_key = "pk\_test\_4PRPYs3eM4HQx0ZMBOubGjoy"-- Don't worry, this is a public test key ;) cardNumber = "4242 4242 4242 4242" fullName = "Test Name" expMonth = "07" expYear = "2016" cvc = "432" ------------------------------------------------------------ firstCard = {["number"] = cardNumber, ["exp\_month"] = expMonth, ["exp\_year"] = expYear, ["cvc"] = cvc, ["name"] = fullName} StripeNewRegister = function () local json = require "json" print ("test") newCustomer = {["email"] = email, ["description"] = description} --["card"] = firstCard} print(newCustomer) local function networkListener( event ) if ( event.isError ) then print( "Network error!" ) else print( "RESPONSE: "..event.response ) local data1 = event.response local resp1 = json.decode(data1) print(resp1) local error = resp1.error if error ~= nil then for i = 1, #resp1.error do print(resp1.error[i].type) print(resp1.error[i].message) end end if error == nil then -- Print Functions end end end local key = {["Bearer"] = api\_key} local headers = { ["Authorization Bearer"] = api\_key } local params = {} params.headers = headers params.body = json.encode( newCustomer ) print( "params.body: "..params.body ) network.request( "https://api.stripe.com/v1/customers", "POST", networkListener, params) end StripeNewRegister()

Not sure if Stripe follows the same pattern, but in other “auth-bearer” apps I’ve seen you use something like this:

headers["Authorization"] = "Bearer " .. some\_auth\_token

i.e. the “Bearer” part is not in the header key.

@jbp1  - Thanks for your reply. I just tried that but I get an API Auth. error. I was getting the same error before when attempting other remedies; I think it likes this format: 

 local headers = { ["Authorization Bearer"] = api\_key }

Still not sure why my http request is invalid.

Are you sure that the body of the post request is supposed to be JSON data and not HTTP POST type key-value pairs?

Rob,

It may be HTTP POST type. How would I do that with lua?

It depends on what the web server script is expecting.  I don’t know Stripe’s API.  They should tell you what they expect in the body.  Many traditional HTTP POST scripts expect key-value pairs. much like GET, i.e.:

key1=value1&key2=value2&key3=value3

These values typically need to be URL encoded (not the whole string, just each value entry).  URL encoded is not Base64 encoding.   The difference between GET and POST is get is delivered on the URL string, which limits the total length of the URL.  POST reads the data from the stdin stream (the socket connecting your client to the server).

I would also check with Stripe and see if they have a way to provide more details about what’s failing.  This isn’t something we can support since none of us use it and have no experience with it.

Rob

I contacted stripe and resolved the issues. It turns out jpb1 was right…I just had to include some other things to make it work. Heres my working code: http://code.coronalabs.com/code/stripe-lua 

Also, thank you for your help Rob, I implemented your suggestion with success. 

I’m glad you got it worked out…

Not sure if Stripe follows the same pattern, but in other “auth-bearer” apps I’ve seen you use something like this:

headers["Authorization"] = "Bearer " .. some\_auth\_token

i.e. the “Bearer” part is not in the header key.

@jbp1  - Thanks for your reply. I just tried that but I get an API Auth. error. I was getting the same error before when attempting other remedies; I think it likes this format: 

 local headers = { ["Authorization Bearer"] = api\_key }

Still not sure why my http request is invalid.

Are you sure that the body of the post request is supposed to be JSON data and not HTTP POST type key-value pairs?

Rob,

It may be HTTP POST type. How would I do that with lua?

It depends on what the web server script is expecting.  I don’t know Stripe’s API.  They should tell you what they expect in the body.  Many traditional HTTP POST scripts expect key-value pairs. much like GET, i.e.:

key1=value1&key2=value2&key3=value3

These values typically need to be URL encoded (not the whole string, just each value entry).  URL encoded is not Base64 encoding.   The difference between GET and POST is get is delivered on the URL string, which limits the total length of the URL.  POST reads the data from the stdin stream (the socket connecting your client to the server).

I would also check with Stripe and see if they have a way to provide more details about what’s failing.  This isn’t something we can support since none of us use it and have no experience with it.

Rob

I contacted stripe and resolved the issues. It turns out jpb1 was right…I just had to include some other things to make it work. Heres my working code: http://code.coronalabs.com/code/stripe-lua 

Also, thank you for your help Rob, I implemented your suggestion with success. 

I’m glad you got it worked out…