NodeJS url.parse( url ).query - node.js

In the nodejs documentation:
query: Either the 'params' portion of the query string, or a querystring-parsed object.
Example: 'query=string' or {'query':'string'}
Link: NodeJS URL
This part is confusing.
When will 'query=string' happens?
When will this
{'query':'string'} also happens?
I have seen that when I do url.parse() it automatically converts the parameters into an object. My code will be buggy if I only support one format.
How will I know if url.parse() converts the parameters in this format: 'query=string'?

url.parse(urlStr, [parseQueryString], [slashesDenoteHost])
If you pass true as the second argument it will also parse the query string using the querystring module and you will get an object {'query':'string'}, otherwise the query string will not be parsed (default behavior) and you will get query=string.

Related

Specify the type of parameter for DELETE request in Nodejs

I would like to do a DELETE request with unspecified number of parameters a=someValue. There is 2 main ways of supplying parameters to my understanding
Query parameters. ?a=someValue . This approach turn everything into
string and since I allow any number of parameters, I cannot know
which one is String, Boolean or Integer
Parameters in Body.This approach goes against the spec of DELETE operation to not have a body. Some server even strip away the body-content. But as I
send an json object, user can specify which type of value each of
their parameters has.
What would be your approach for this?
I'd use query parameters over body as the DELETE method has an optional body. Some clients may choose to ignore the body totally.

How can I check if there is a specific parameter in a sails.js request?

How can I check whether there is a specific parameter inside req.param() in sails.js?
For example, I want to check whether the parameter X_id exists in req.param(). If the parameter exists, then I want to use it. If it doesn't exist, then I want to use a default value instead. I've tried the following code:
X_owner: req.param('X_id') || -1,
But I receive an error when I run this code with out X_id parameter. How can I fix the code?
If you're aiming to use default values for your parameters, I'd suggest using actions2 if you haven't tried. It lets you define parameters (using the inputs' object) and use defaultsTo on inputs you want to have default values for, just like in your models attribute definitions. Using these should help resolve the issue.
As for how to check whether there is a specific parameter inside req.param(), you can use req.allParams() to reveal all current parameters from all sources. These include parameters parsed from the url path, the request body, and the query string in that order.

Swagger API "required" - how much is this required?

I've created a Swagger (on nodejs/express) test API with this specification (only relevant part):
...
parameters:
- name: name
in: query
required: true
type: string
...
But I can call the url with empty paramter, for example
http://localhost/test?name=
And it works without any problem, throws no exception or any other sign. Why?
If I make a similar call from the terminal via curl or via postman, it works as well. I parsed the query from the request object and found that in this case, the query parameter is interpreted as an empty string.
Making the call via SwaggerUI is different though, as the UI will actually not make the call UNLESS the query field has a value.
Try doing console.log(req.query); in your handler. You will probably see {name: ''}. Which is legitimate, just that the value of name is an empty string.
Look at JSON4 here: Representing null in JSON. So name IS defined, but it's empty.
You will probably need to do a check for empty string values.
I hope this helps!

gatling combining transform with jsonpath

I'm trying to extract a security_token from this response :
{}&&{"containers":{"userID":"p8admin","connected":true,"desktop":"icm"},
"userid":"p8admin",
"user_displayname": "p8admin",
"security_token":"-1829880900612241155",
"messages":[{"adminResponse":null,
"moreInformation":null,
"explanation":null,
"number":"0",
"userResponse":null,
"text":"p8admin connect\u00e9."
}]
}
I've tried combining transform and jsonPath :
.check(bodyString.transform(_.split("&&")(1)).jsonPath("&.security_token").saveAs("security_token"))
but i get this error :
value jsonPath is not a member of com.excilys.ebi.gatling.core.check.MatcherCheckBuilder
Let me know if there is a simple way to achieve this.
Thanks
From the documentation on checks:
This API provides a dedicated DSL for chaining the following steps:
defining the check
extracting
transforming
verifying
saving
Since the response isn't valid JSON, you'll need to use bodyString as the type. You can then transform and then save, but you can't go back to step 1. You can parse the value you need out of the JSON during the transform step.
As Stéphane pointed out, the easiest way to get the value is to use a regex check and extract the security_token value directly, as long as you don't need the rest of your JSON object for any logic.
I have the same problem, I used the regex() function like this :
.check(regex(""""security_token":"(.*)",""").saveAs("security_token"))

Using 'querystring.parse' built-in module's method in Node.JS to read/parse parameters

Scenario:
Consider the following code:
var querystring = require('querystring');
var ParamsWithValue = querystring.parse(req._url.query);
Then I am able to read any query string's value.
E.g: If requested string is http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324
I can get the values of query string with codes ParamsWithValue.UID & ParamsWithValue.FacebookID respectively.
Issue: I am able to get the values of any number of parameters passed in the same way described above. But for second time onwards I am getting the following error in response on browser.
Error:
{"code":"InternalError","message":"Cannot read property 'query' of undefined"}
Question: What is wrong in the approach to read the query string from the URL.
Note: I don't want to use any frameworks to parse it. I am trying to depend on built-in modules only.
Update: It responds correctly when the value of any of the parameter is changed. But if the same values requested again from even different browser it throws same error.
I think you need req.url rather than req._url.
req.url is a string, if you want a URI instance use require('url').parse(req.url)
So, you should finally have:
var ParamsWithValue = querystring.parse(require('url').parse(req.url).query);
Edit: I corrected a typo in point 1, the last req.url -> req._url

Resources