Escaping Characters - MongoDB problem

Hi!

I am using MongoDB (via Amazon EC2) to serve data to my app - the problem I am having is if a particular word has  -  's  - in it, it causes major problems. Any idea how how I should be escaping these characters on the server end, while leaving the word with 's in tact on the front end?

Thanks!

Hi,

Maybe this will help:

https://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

What you probably want to do is to escape() your strings before you send them to the app. In JavaScript would use escape()/unescape() to encode and decode the values.

I’m assuming you’re sending a JSON response from your server, so this answer would be applicable to that. What is happening is that the JSON strings are enclosed by a tick ‘, and if your string has an apostrophe in it (’) it breaks the string:

{ 'myProperty': 'Todd's stuff' }

In that example, the string “Todd’s stuff” is terminated at the second apostrophe and the JSON becomes invalid because “s stuff” is not valid JSON.

If you’re not sending JSON, can you provide us your response data?

Albert

Hi, Thanks for your response!

So here is what I am trying to do. I am trying to process a stripe charge, and keep getting this error:

STRIPE PLUGIN ERROR:
Stripe returned an error code. Here are the details Stripe provided:
HTTP status code: 400
message: Received unknown parameter:  Rusty’s
type: invalid_request_error
param:  Rusty’s

 

As you can see, if the word has an ’ in it, it gives me an error, otherwise the sale goes through fine. Stripe is grabbing the name from my mongoDB:

 

sample:

 

{

name: “Rusty’s”,

title: “Rusty’s”,

stripeName: “Rusty’s”,

starUnselected: “images/star.png”,

matchingId: “af68bdc58d”,

}

 

I manually enter data into Mongo tables. 

 

Thoughts?

 

Thanks!

 

Can you print the response to the console window? I think the response may be using a single quote instead of double quotes.

Actually, can you post some code?

I’m just thinking out loud here, but your HTTP Status Code is 400 in that error. 4xx status codes are error codes returned by servers, so I get the feeling that this may be an error in your server code. Can you double check that?

Status code 400 is a specific error called “malformed request”. Basically the HTTP request coming in couldn’t be parsed into an endpoint and query key-value pairs (or the body was messed up on a POST request.  Since Coronimum is used by many people, I’m pretty sure it’s creating valid requests. What’s left is the data the request is sending.

See this post on the problem. It doesn’t provide a Lua answer, but the code can be easily translated to our string.* library (or better use the UTF8 plugin if you’re going to have international characters).

http://stackoverflow.com/questions/770523/escaping-strings-in-javascript

EDIT: Forgot to post the URL!

Rob

Thanks! Will take a look into it. 

Hi! So it’s definitely a problem with having " 's " in a word that I am trying to POST. I am trying to figure out how to escape the ’ to be able to send the word successfully. I am trying to use the UTF8 plugin, but not sure what function to use for this purpose? (I am not using international characters). If I were to use the php solution from that link you sent, can you perhaps please give me some guidance? Thanks!

Hi,

Are you able to try escaping the apostrophe manually? For example. do a find and replace in the string to change " ’ " into " ’ "?

I am POSTing this: 

name = tostring(storeMenu[i].stripeName)

The storeMenu[i].stripeName is being pulled from MongoDB, however Stripe does not like the 's and spits me back an error. If the name has no ‘s, then the transaction goes through fine. I can apply a find and replace to that I guess by doing the following: name = string.gsub( storeMenu[i].stripeName, "’", “/” )

Would that work?

trying this now: string.gsub( storeMenu[i].stripeName, “’”, " ’ " )

Will keep you posted…

still not working… aargh

AAAH it wasn’t the 's that Stripe was not liking… it was the & in the name. All fixed! Thanks for your help though!

Hi,

Maybe this will help:

https://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

What you probably want to do is to escape() your strings before you send them to the app. In JavaScript would use escape()/unescape() to encode and decode the values.

I’m assuming you’re sending a JSON response from your server, so this answer would be applicable to that. What is happening is that the JSON strings are enclosed by a tick ‘, and if your string has an apostrophe in it (’) it breaks the string:

{ 'myProperty': 'Todd's stuff' }

In that example, the string “Todd’s stuff” is terminated at the second apostrophe and the JSON becomes invalid because “s stuff” is not valid JSON.

If you’re not sending JSON, can you provide us your response data?

Albert

Hi, Thanks for your response!

So here is what I am trying to do. I am trying to process a stripe charge, and keep getting this error:

STRIPE PLUGIN ERROR:
Stripe returned an error code. Here are the details Stripe provided:
HTTP status code: 400
message: Received unknown parameter:  Rusty’s
type: invalid_request_error
param:  Rusty’s

 

As you can see, if the word has an ’ in it, it gives me an error, otherwise the sale goes through fine. Stripe is grabbing the name from my mongoDB:

 

sample:

 

{

name: “Rusty’s”,

title: “Rusty’s”,

stripeName: “Rusty’s”,

starUnselected: “images/star.png”,

matchingId: “af68bdc58d”,

}

 

I manually enter data into Mongo tables. 

 

Thoughts?

 

Thanks!

 

Can you print the response to the console window? I think the response may be using a single quote instead of double quotes.

Actually, can you post some code?

I’m just thinking out loud here, but your HTTP Status Code is 400 in that error. 4xx status codes are error codes returned by servers, so I get the feeling that this may be an error in your server code. Can you double check that?

Status code 400 is a specific error called “malformed request”. Basically the HTTP request coming in couldn’t be parsed into an endpoint and query key-value pairs (or the body was messed up on a POST request.  Since Coronimum is used by many people, I’m pretty sure it’s creating valid requests. What’s left is the data the request is sending.

See this post on the problem. It doesn’t provide a Lua answer, but the code can be easily translated to our string.* library (or better use the UTF8 plugin if you’re going to have international characters).

http://stackoverflow.com/questions/770523/escaping-strings-in-javascript

EDIT: Forgot to post the URL!

Rob