How to code one (RESTful) route with more than one parameter - javascript

My node server uses one and only one route: GET /I/want/title
This route expects a list of websites addresses in query string format e.g.
/web/=http://bing.com
/web/?address=http://yahoo.com
This works ok, so how to use one route for both or more?
yahoo&bing
Only one route will be used for both single and multiple requests.

You could use, for example:
/I/want/title/?address=bing.com,www.yahoo.com
And then easily split parameter to array:
var addressesArray = address.split(',');

Related

Axios get one parameter with multiple values separated by comma

I have a table in React with data about tests which I get from API. I have to do filters on frontend and be able to join them, for example filter tests from chosen category, chosen difficulty and created before some data.
If I wanted to filter for example tests from category "Javascript" and difficulty "Junior", I should get the uri:
/api/admin/filters?filter=category:Javascript,difficulty:JUNIOR
If I wanted to filter tests for "Junior" or "Mid" I should get:
/api/admin/filters?filter=difficulty:JUNIOR,difficulty:'MID
Note apostrophe here which stands for "or".
I should also be able to filter by creation date, for example:
/api/admin/filters?filter=creationDate<2019-09-23 17:34:21,creationDate>2019-09-12 17:34:21
I wonder how I can create such queries? URLSearchParams or axios params adds parameters separated by & so I can't use it here because I have one parameter "filter" with multiple values. Or maybe I should use it and also use js replace method for replacing & for comma? I have also no idea how to add apostrophe.
I saw similar question here: https://spectrum.chat/react/general/query-string-sending-multiple-value-to-the-same-parameter~5d029da0-e7da-443d-a4b2-1529ca7b4f82
but I can't use an array in my case because I have multiple filters. I suppose I have to create object like:
options = {
difficulty: [junior, mid],
category: [javascript],
created before: 2019-09-23 17:34:21,
created after: 2019-09-12 17:34:21
}
and now how to add keys and values from such object to uri so it looks like backend expects?
Any help would be appreciated.
Encoding parameters with encodeURI before passing to axios might solve your issue.
If you need to pass parameters with special characters (like '[',']' ...etc) you should give the parameter as a string '["junior","mid"]' instead of giving parameter as an array.
(Giving as an array will just remove the brackets)
var params = '["junior","mid"]'
encodeURI(params) // it returns "%5B%22junior%22,%22mid%22%5D"
var params = ["junior","mid"]
encodeURI(params) // it returns "junior,mid"

What is the difference between 'path' and 'query' in a API call

I m trying to implement faceit API in my website.There are two end points from which I can retieve a player's information
1.https://open.faceit.com/data/v4/players?nickname=DeADLY2501&game=CSGO&game_player_id=76561198806878477
This is the first way.Below i will link a image.From faceit documentation(https://developers.faceit.com/docs/tools/data-api)
Here there is a parameter called 'game_player_id' which says it must be a string and 'query'.
Here is the second endpoint
Here in which a parameter is need called 'player_id' which is required needs to be a string and must be a 'path'.
Can some one please tell me what is the difference.Because in the first end point.We need 'nickname','game' and 'game_player_id'.
I just want to retrive a players information just from the id,So that can be made possible by the second endpoint.The problem is that.With the same 'player_id',I send calls for both end points.The first one sends a response successfuly.While the second endpoint says 'Not Found'.I gather that it might be that the type of parameter im making the request with are not proper for the 2nd endpoint.
Any help regarding this is appriciated thank you.
There is a formal definition here
But sometimes it's easier to see an example, consider https://stackoverflow.com/questions/62362966/?arg=3;other=word This is composed of several parts:
https - this is the scheme
stackoverflow.com - this is the hostname
questions/62362966/ - this is the path
arg=3;other=word - this is the query
Note that where your documentation above refers to a value appearing in a path or query, that does not mean it exclusively constitutes the path or query - there is also data/meta-data framing it.
Path and query - different parts of the URL. Basically, everything that goes after ? is query, everything between domain and ? is path.
See details here
In example below I mean the user_id is a path.
https://www.example.com/api/v2/users/{user_id}
https://www.example.com/api/v2/users/11
In example below I mean the limit and the page is a query.
https://www.example.com/api/v2/users?limit={limit_value}&page={limit_value}
https://www.example.com/api/v2/users?limit=50&page=1
A query goes after ? AND is made up of a key and a value in the form key=value. Each pair is separeted by a &.
A path is what goes after the first slash (/) and before ?. It doesn't require the key-value pattern. You just put the value in the right spot (i.e. between other slashes).

How to get content from all nested/linked documents in single query to Prismic?

I know that it's possible to pass the config object as the second parameter and defined which fields I need but it's really inconvenient because I have a lot of them:
client.query(
Prismic.Predicates.at('document.type', 'my_page'),
{'fetchLinks': ['section1.title', 'section1.subtitel', 'section2.title', 'section2.image', .......]}
);
Is it possible to get data(content) of all nested fields for linked documents in a single query?
Try using the new GraphQL API: https://prismic.io/docs/rest-api/query-the-api/graphquery
Slightly tedious but works!

Issue with picking multiple JSON

I have recorded a script and "search" an id from it.
I have performed the following things
Have parametrize the "searcID" so that it can be picked from the "CSV_Data Config"
Have extracted the "key" from the URL through "Regular Expression Extractor" to provide it to the desired URL requiring "key", so that it can be dynamic
Now the issue is, since the script is recorded for one search id, in "/build-4.4.10.0/SECChecker/Search/Html?_dc=0.5557150364018139&Grid-Ajax", the last line of my script has a body of the one recorded "searchId".
The script runs and return for each thread this same result of JSON (that is present in the last line i mentioned), i want this too to be dynamic, how can i do that? Please guide
If you want to parametrize the last request, you should use the following notation: ${"var name"} and use a CSV manager. link2
for instance, if you want to parametrize the fist param of the body you should have something like this:
{"SortField":"${var_name}",....
One thing, the dc param, part of the path, looks like a random used to avoid cache, so I use this to simulate the requests during the test:
.../Html?_dc=${__RandomString(15,0123456789)}&Grid-Ajax
This function returns a string which its length is 15 (1st param) and has a set of numbers (2nd param)
Hope It helps you.

Is there a way to determine in angular.js if there are url query parameters present?

In angular routes, you can parse the url arguments into $routeParams. However, I need to know what's the best way to see if they exist.
For example, I have several $routeParams from different parts of the URL however, all I want to know is whether any url params exist (if the ? exists and parameters after). I understand, I can just do a search for the ? but wanted to know if there was a way within angular.
Here's my code:
$routeProvider.when('/sports/:sport_id/:args?',
sport_id and all the args will be combined in $routeParams. However I just want to know if args exist. (the query parameters)
You can inject $routeParams and look for the presence of any properties in the object.
ex: var hasParams = Boolean(Object.keys($routeParams).length);
You dont need the Boolean( part as 0 will be falsy anyways, however just being more explicit.
If looking for just the query string params you can just inject $location service and use $location.search() it will provide object with query string argument as key value pair on the object.

Categories

Resources