react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

How do I prevent escaping of '/' character in config params?

Open plell opened this issue 7 years ago • 18 comments

For any '/' character included in a param string, the plugin returns '\ /' (with no space). How can I escape the '/'? I tried using erb literal quoting with %q{...} to avoid trouble with slash (as recommended here https://ruby-doc.org/stdlib-2.6.3/libdoc/erb/rdoc/ERB.html) to no avail.

  • Plugin version:3.0.1
  • Platform: Android
  • OS version: 9
  • Device manufacturer / model: Pixel 3
  • React Native version: 0.53.0
  • Plugin config foregroundService:true, enableHeadless: true, stopOnTerminate: false, enableTimestampMeta: true, allowIdenticalLocations:true, distanceFilter: 0, locationUpdateInterval:10000, desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH, stopTimeout: 1, debug: true, logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE, stopOnTerminate: false,
    startOnBoot: true, url: 'http://requestbin.fullcontact.com/18jlmel1' batchSync: false, autoSync: true, params: {
    "myUrl": messages/3456, } }

Expected Behavior

the "topic" string in params is sent to database as is: "messages/3456"

Actual Behavior

the "topic" string in params is sent to database with an added character: "messages\ /3456". Each '/' included in a string for locationTemplate or params returns '\ /' (no space)

Steps to Reproduce

  1. add "test":"my/test" to your params

Context

I need to send a url as a parameter. For every '/', the plugin returns '\ /'

Thanks for the help!

plell avatar Apr 27 '19 07:04 plell

Try ”my///test”

christocracy avatar Apr 27 '19 11:04 christocracy

i tried this and got ”my \ / \ / \ / test” (no spaces)

this comment editor was removing the extra character in my report, so i'll re-clarify the issue here (with spaces added). when I put "my/test" as a param, it becomes "my\ /test".

plell avatar Apr 27 '19 18:04 plell

Google your escaping issue in the context of “NSString escape backslash”

christocracy avatar Apr 28 '19 05:04 christocracy

Looks like I phrased my issue in reverse, sorry. I want to prevent the escaping of forward slash.

Here are two threads on the issue: https://stackoverflow.com/questions/19651009/how-to-prevent-nsjsonserialization-from-adding-extra-escapes-in-url https://stackoverflow.com/questions/15794547/nsjsonserialization-serialization-of-a-string-containing-forward-slashes-and-h

All the solutions I've found have to do with replacing instances of "\ /" with "/". In this case, it seems like that has to happen within the plugin.

Is there a place in config that I can create a rule like this? Str = [Str stringByReplacingOccurrencesOfString:@"\ /" withString:@"/"];

plell avatar Apr 29 '19 05:04 plell

There shouldn't be an issue with / in #params or #extras. Escaping should only be required if you're using a custom locationTemplate.

I have tried this myself on my test-server:

BackgroundGeolocation.ready({
      reset: true,      
      url: 'http://192.168.11.22:9000/locations',
      params: {
        "myUrl": "http://foo.bar.com/messages/3456"
      }
    })

Server

app.post('/locations', async function (req, res) {
    var data = req.body;  // <-- here's the POST data from plugin, already JSON-decoded
    console.log('- data.myUrl'.green, data.myUrl);
    console.log('POST /locations_template\n%s\n'.green, JSON.stringify(data, null, 2));

    res.send({ success: true });
  });
- location.myUrl http://foo.bar.com/messages/3456

POST /locations_template
{
  "myUrl": "http://foo.bar.com/messages/3456",
  "location": {
    "coords": {
      "speed": 0,
      "longitude": -122.0312186,
      "floor": 0,
      "latitude": 37.33233141,
      "accuracy": 5,
      "altitude_accuracy": -1,
      "altitude": 0,
      "heading": -1
    },
    "extras": {},
    "is_moving": false,
    "event": "motionchange",
    "odometer": 1657.5,
    "uuid": "76589FEE-8503-4CED-B62B-6F1B8BCB727F",
    "activity": {
      "type": "unknown",
      "confidence": 100
    },
    "battery": {
      "level": -1,
      "is_charging": false
    },
    "timestamp": "2019-04-29T12:45:02.121Z"
  }
}

christocracy avatar Apr 29 '19 12:04 christocracy

Here's the result with using a locationTemplate (again, no escaping required for data within params:

BackgroundGeolocation.ready({
      reset: true,      
      locationTemplate: '{"latitude":<%= latitude %>, "longitude":<%= longitude %>}',
      url: 'http://192.168.11.22:9000/locations',
      params: {
        "myUrl": "http://foo.bar.com/messages/3456"
      }
    }).then((state) => {

Server

- location.myUrl http://foo.bar.com/messages/3456

POST /locations_template
{
  "myUrl": "http://foo.bar.com/messages/3456",
  "location": {
    "longitude": -122.0312186,
    "latitude": 37.33233141
  }
}

christocracy avatar Apr 29 '19 12:04 christocracy

Adding myUrl into locationTemplate:

BackgroundGeolocation.ready({
  reset: true,      
  locationTemplate: '{"latitude":<%= latitude %>, "longitude":<%= longitude %>, "myUrl":"http://foo.bar.com/messages/789"}',
  url: 'http://192.168.11.22:9000/locations',
  params: {
    "myUrl": "http://foo.bar.com/messages/3456"
  }
})

Server

- data.myUrl: http://foo.bar.com/messages/3456
- data.location.myUr:l http://foo.bar.com/messages/789

POST /locations_template
{
  "myUrl": "http://foo.bar.com/messages/3456",
  "location": {
    "foo": "bar",
    "latitude": 37.33233141,
    "longitude": -122.0312186,
    "myUrl": "http://foo.bar.com/messages/789"
  }
}

christocracy avatar Apr 29 '19 13:04 christocracy

hm! I'm envious now; I'm still seeing added backslashes. You can see my posts at https://requestbin.fullcontact.com/1gt75p81?inspect

locationTemplate: `{"payload":'{"timestamp":"<%= timestamp %>","position.latitude":<%= latitude %>,"position.longitude":<%= longitude %>,"uuid":"<%= uuid %>","position.speed": <%= speed %>,"position.altitude": <%= altitude %>,"position.direction": <%= heading %>,"activity":"<%= activity.type %>","odometer":<%= odometer %>,"battery":<%= battery.level %>,"event":"<%= event %>"}'}`,
httpRootProperty:'.',
url: 'http://requestbin.fullcontact.com/1gt75p81',
params: {
        "topic": "messages/w45ye5ye",
        "myUrl": "http://foo.bar.com/messages/3456",
        "type": 2
      }

I appreciate your time, thank you!

plell avatar Apr 29 '19 17:04 plell

You wrap code in triple backticks for multi-line syntax highlighting`, not one.

christocracy avatar Apr 29 '19 17:04 christocracy

Thanks ^^

plell avatar Apr 29 '19 17:04 plell

The problem is your locationTemplate. Look what you're doing:

{"payload": '{"payload":'{"timestamp":"<%= timestamp %>"...}'

You've stated that the value of payload is a String!

christocracy avatar Apr 29 '19 17:04 christocracy

What you should be doing is using httpRootProperty:

BackgroundGeolocation.ready({
  httpRootProperty: 'payload',
  locationTemplate: '{"lat": <%= latitude %>, "lng": <%= longitude %>}'
});

christocracy avatar Apr 29 '19 17:04 christocracy

I want payload to be a stringified object, so as far as output, that's exactly how i want it.

It's only the params, "myUrl" and "topic", that need unescaping.

plell avatar Apr 29 '19 17:04 plell

You're putting JSON into JSON. You're on your own.

christocracy avatar Apr 29 '19 17:04 christocracy

That isn't relevant to this issue. I posted with your exact example and get escaped forward slashes.

BackgroundGeolocation.ready({
      reset: true,      
      url: 'https://requestbin.fullcontact.com/1gt75p81',
      params: {
        "myUrl": "http://foo.bar.com/messages/3456"
      }
    })

You can see it here: https://requestbin.fullcontact.com/1gt75p81?inspect

plell avatar Apr 29 '19 17:04 plell

I am having this exact same problem actually (trying to post to a server i do not control). One of the fields that the server expects has a “/“ in the value, which is escaped to “\ /“. Is there any way to turn off escaping in locationTemplate?

Evanfeenstra avatar Apr 29 '19 18:04 Evanfeenstra

This isn’t a problem of escaping. This is the natural behavior any JSON parser when you place JSON into JSON.

christocracy avatar Apr 29 '19 18:04 christocracy

You can test this yourself in the Javascript console:

data = {"payload": '{"foo": "/my/test"}'}
json = JSON.stringify(data)
data = JSON.parse(json)
payload = JSON.parse(data.payload)

christocracy avatar Apr 29 '19 19:04 christocracy