1

I am using expressjs in nodejs for creating my api. Now when I am sending a string as "Wed Jul 29 2015 17:34:22 GMT+0830" in the get request in the querystring and parsing it as url.parse(uri, true).query it gives me a string as "Wed Jul 29 2015 17:34:22 GMT 0830" without the '+'. How can I get the + in the string

Updating with the code

getEventTimeFromurl: function(ctx){

        var decodeURI = decodeURIComponent(ctx.req.url);
        var eventTime = undefined;
        const strEventTime = "eventTime=";

        var eventTimeindex = decodeURI.search(strEventTime);
        if (eventTimeindex == -1)
            throwError.badRequest(ctx, 101, 'Invalid event time');

        var aftereventtime = decodeURI.substring(eventTimeindex + strEventTime.length);
        var endIndex = aftereventtime.indexOf('&');
        if (endIndex == -1)
            eventTime = aftereventtime;
        else
            eventTime = aftereventtime.substring(0, endIndex);

        return eventTime;

    }

Here eventTime= is in the query string which have '+' sign like 'Mon Aug 03 2015 13:27:24 GMT+0930'.

2
  • So to be clear: the getEventTimeFromurl function is a solution to your problem, correct? Commented Aug 4, 2015 at 14:06
  • yes, this is what I tried and worked. a workaround Commented Aug 4, 2015 at 14:12

2 Answers 2

1

You can encode string to URI format.

var dateString = "Wed Jul 29 2015 17:34:22 GMT+0830";
var encodedString = encodeURIComponent(dateString);

use encodedString to pass it in the query string.

Sign up to request clarification or add additional context in comments.

5 Comments

Since the string is data which is passed by the client api, so I cannot encode or decode the date strings. Can I do something on the server side to get back the + that is being sent
No, you can not do anything on server side when you are sending plus keyword in querystring. server automatically consider it as a space.
You can use unix like timestamp variable to pass date if your client api provides it
No, its a format in string, and the apis are already released so it cannot be done on the client side. I need to parse it on the server side.
Try to use this. and try something to get it what you want :new Date( req.query.queryStringName)
0

Instead of having url.parse() parse the query string, you can first replace any + characters in it by their URL-encoded version (%2b) and then parse the query string manually:

var qs = require('querystring');

var querystring = url.parse(uri).query.replace(/\+/g, '%2b');
var query       = qs.parse(querystring);

Although this assumes that all + characters in the query string need to be kept. If that's not acceptable, you can probably modify the regular expression to match only specific cases (like in timezone notations).

EDIT: combined with Express, it would look something like this:

app.get('/', function(req, res) {
  var querystring = url.parse(req.url).query.replace(/\+/g, '%2b');
  var query       = qs.parse(querystring);
  ...
});

5 Comments

I took the url from the request, decoded it, and searched for the parameter in the string.
@Nipun what do you mean?
var decodeURI = decodeURIComponent(ctx.req.url); var eventTime = undefined; const strEventTime = "eventTime="; var eventTimeindex = decodeURI.search(strEventTime); if (eventTimeindex == -1) { var aftereventtime = decodeURI.substring(eventTimeindex + strEventTime.length); var endIndex = aftereventtime.indexOf('&'); if (endIndex == -1) eventTime = aftereventtime; else eventTime = aftereventtime.substring(0, endIndex);
@Nipun please update your post with your code, comments aren't meant to post code in. Also explain a bit more, like what ctx is.
Here I am getting the decoded URI. Now from the URI I extract the time string from the query string and then use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.