2

I have the following variables in javascript

var domain= http://example.com/api/v1/purchases/get_purchases.json?
var header = 'df0a96ddbe1ada9fda4b1ed9b02cf67c'
var params = {
                   search:{
                         status_eq: 'frozen',
                         email_eq: 'a@a.com',
                         phone_number_eq: ''
                   }
                  }
var api_string = domain + header + params;

I need output of api_string to be:

http://example.com/api/v1/purchases/get_purchases.json?headers%5B_token%5D=df0a96ddbe1ada9fda4b1ed9b02cf67c&search%5Bemail_eq%5D=a@a.com&search%5Bphone_number_eq%5D=12345&search%5Bstatus_eq%5D=frozen

I tried JSON.stringify, encodeURI, encodeURIComponent but it doesnt work as expected.

I need to use this api_string in fetch function of react-native.

subha
  • 426
  • 1
  • 3
  • 14

3 Answers3

1

You could use a serializing function, like so:

serialize = function(obj, prefix) {
  var str = [], p;
  for(p in obj) {
    if (obj.hasOwnProperty(p)) {
      var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
      str.push((v !== null && typeof v === "object") ?
        serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
  }
  return str.join("&");
}

console.log(serialize(params));

Gives:

search%5Bstatus_eq%5D=frozen&search%5Bemail_eq%5D=a%40a.com&search%5Bphone_number_eq%5D=`
Stuart
  • 6,630
  • 2
  • 24
  • 40
0

Good news, string concatination can be done with just the + operator. So you can do something like this

let result = domain + "headers%5B_token%5D=" + header + "&search%5Bemail_eq%5D=" + params.search.eqmail_eq

(obviously extend this for the rest of the parameters, assuming you only have those three params ever)

TheCog19
  • 1,129
  • 12
  • 26
  • im trying to write a function that will handle params of any type.. so this wil not be helpful. Thankyou for ur help anyway... – subha Aug 22 '17 at 13:04
0

Have you tried using the npm package url ?

This is an example of how it works:

    const url = require('url')

    let my_url = url.format({
          hostname: "mydomain.com",
          protocol: "http",
          query: {status_eq: 'frozen', email_eq: 'a@a.com',},
          pathname: '/my/path'
    })

here is the documentation if you need to know more about how it works.

WARNING

url.format takes in an object of the parameters you want to send to the server. Make sure that if one of the parameters you want to pass is an object you JSON.stringify it before adding it to the query. Here is a stackoverflow question that explains it.

Nicola Pedretti
  • 4,831
  • 3
  • 36
  • 42
  • mmm, weird, could you comment with the object you are trying to format? – Nicola Pedretti Aug 22 '17 at 13:26
  • this is the object im trying.. var params = { search:{ status_eq: '', email_eq: 'a@a.com', phone_number_eq: '' } } – subha Aug 23 '17 at 08:42
  • I see what is going on. You are passing an object as a value. In that case you need to JSON.stringify the object that you pass to search. I will make an update to my answer in case more people are having this problem! – Nicola Pedretti Aug 23 '17 at 12:30
  • The params object is result of an API call.. I will not know the levels of nesting in it.. how do I handle this? – subha Aug 23 '17 at 13:02
  • 1
    JSON.stringify will take care of any nested level. You need to call it on the first layer. So in your case whatever you pass to search. – Nicola Pedretti Aug 23 '17 at 13:03