Duplicate parameters in Angular $http - javascript

I want to add multiple parameters with the same name to a request URL. I'm using Angular's $http.
The URL should look like this:
http://myBaseUrl?name1=value1&name1=value2...
I know that it is possible to make something like this when I set the values as an array:
http://myBaseUrl?name1=value1,value2...
But it has to be like the first one.

If you're using HttpClient you can use HttpParams for this.
let params = new HttpParams();
// Assign parameters
params = params.append('firstParameter', 'valueOne');
params = params.append('firstParameter', 'valueTwo');
// Get request
this.http.get(`http://example.com`, { params }).subscribe();

Related

how to pass a varible in api link url in vue js

In the below API i wanted to replace chicken with my ingredient variable value.
var ingridient = ref("chicken");
https://api.edamam.com/api/recipes/v2?type=public&q=chicken&app_id=MY_API_ID&app_key=MY_API_KEY
You can use String.prototype.replace()
https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/String/replace
var url = "https://api.edamam.com/api/recipes/v2?type=public&q=chicken&app_id=MY_API_ID&app_key=MY_API_KEY";
console.log(url);
url = url.replace("chicken","grilledChicken");
console.log(url);
In your case: url = url.replace("chicken", ref("chicken"));
As i was using composition API of Vue JS for that reason only ${ingridient} was not working for me. In composition API any variable with reactive ref provides an object. so for retrieving my ingridient value i need to call the value by ${ingridient.value}.
var ingridient = ref("chicken");
https://api.edamam.com/api/recipes/v2?type=public&q=${ingridient.value}&app_id=MY_API_ID&app_key=MY_API_KEY

Query string with fetch javascript

I'm working on a project and i want to define url object that will be passed to fetch ex.
https://t1.testing.com/test/api/v1/blog?pagingindex=0&pagingresults=10
const url_object = {
url: `https://t1.testing.com/test/api/v1/blog`,
url_params: {
method: "POST",
pagingindex: 0,
pagingresults: 10
}
};
and when i call fetch(url_object.url, url_object.url_params) i get an error. How can i incorporate this into fetch? So i need to allow user to define method and query string that will be passed. Thanks in advance!
You can have your cake and eat it too -- easily convert dictionary style objects to query parameters with no fuss:
var url = new URL('https://example.com/');
url.search = new URLSearchParams({blah: 'lalala', rawr: 'arwrar'});
console.log(url.toString()); // https://example.com/?blah=lalala&rawr=arwrar
The object you have labeled url_params is described by the MDN documentation as:
An options object containing any custom settings that you want to apply to the request.
It then goes on to list the properties you can include there.
pagingindex and pagingresults are not among them.
The query string is part of the URL. If you want to put data there, then put it in the URL.
const url_object = {
url: `https://t1.testing.com/test/api/v1/blog?pagingindex=0&pagingresults=10`,
url_params: {
method: "POST"
}
};
You may wish to use the URL object to construct the URL (it will handle escaping for you).

Send unencoded query parameters in Ember

I'm working with an API that only accepts unencoded query parameters. I notice that when I make a query in Ember like so:
this.store.query('listing', {filter: params});
The uri is encoded when it hits the api:
/v1/listing?filter%5Bcategory%5D=123
What I need is for the query parameters to get to my API unencoded, eg:
/v1/listing?filter[category]=123
Can anybody give me a steer on what is the right way to do this in Ember?
So the solution I ended up implementing is not great but it does the trick until I find a better way.
Ember uses Ajax to form the request, so what I did was override 'query' in my JSONAPIAdapter to pass through a custom set of options. My query now looks like this:
query(store, type, query) {
let url = this.buildURL(type.modelName, null, null, 'query', query);
if (this.sortQueryParams) {
query = this.sortQueryParams(query);
}
query = decodeURI(Ember.$.param(query));
let options = {
"processData": false,
"data": query
};
return this.ajax(url, 'GET', options);
},
The key was to stop ajax's automatic processing of the data object, and decode the result of jquery's 'param()' helper function, which is used to convert the query param object into a serialized string.
The result is a decoded query param string like so:
/v1/listing?filter[category]=123

How to get to request parameters in Postman?

I'm writing tests for Postman which in general works pretty easily. However, I now want to access some of the data of the request, a query parameter to be exact.
You can access the request URL through the "request.url" object which returns a String. Is there an easy way in Postman to parse this URL string to access the query parameter(s)?
The pm.request.url.query.all() array holds all query params as objects.
To get the parameters as a dictionary you can use:
var query = {};
pm.request.url.query.all().forEach((param) => { query[param.key] = param.value});
I have been looking to access the request params for writing tests (in POSTMAN). I ended up parsing the request.url which is available in POSTMAN.
const paramsString = request.url.split('?')[1];
const eachParamArray = paramsString.split('&');
let params = {};
eachParamArray.forEach((param) => {
const key = param.split('=')[0];
const value = param.split('=')[1];
Object.assign(params, {[key]: value});
});
console.log(params); // this is object with request params as key value pairs
edit: Added Github Gist
If you want to extract the query string in URL encoded format without parsing it. Here is how to do it:
pm.request.url.getQueryString() // example output: foo=1&bar=2&baz=3
pm.request.url.query returns PropertyList of QueryParam objects. You can get one parameter pm.request.url.query.get() or all pm.request.url.query.all() for example. See PropertyList methods.
It's pretty simple - to access YOUR_PARAM value use
pm.request.url.query.toObject().YOUR_PARAM
Below one for postman 8.7 & up
var ref = pm.request.url.query.get('your-param-name');
I don't think there's any out of box property available in Postman request object for query parameter(s).
Currently four properties are associated with 'Request' object:
data {object} - this is a dictionary of form data for the request. (request.data[“key”]==”value”) headers {object} - this is a dictionary of headers for the request (request.headers[“key”]==”value”) method {string} - GET/POST/PUT etc.
url {string} - the url for the request.
Source: https://www.getpostman.com/docs/sandbox
Bit late to the party here, but I've been using the following to get an array of url query params, looping over them and building a key/value pair with those that are
// the message is made up of the order/filter etc params
// params need to be put into alphabetical order
var current_message = '';
var query_params = postman.__execution.request.url.query;
var struct_params = {};
// make a simple struct of key/value pairs
query_params.each(function(param){
// check if the key is not disabled
if( !param.disabled ) {
struct_params[ param.key ] = param.value;
}
});
so if my url is example.com then the array is empty and the structure has nothing, {}
if the url is example.com?foo=bar then the array contains
{
description: {},
disabled:false
key:"foo"
value:"bar"
}
and my structure ends up being { foo: 'bar' }
Toggling the checkbox next to the property updates the disabled property:
have a look in the console doing :
console.log(request);
it'll show you all you can get from request. Then you shall access the different parameters using request., ie. request.name if you want the test name.
If you want a particular element in the url, I'm afraid you'll have to use some coding to obtain it (sorry I'm a beginner in javascript)
Hope this helps
Alexandre
Older post, but I've gotten this to work:
For some reason the debugger sees pm.request.url.query as an array with the items you want, but as soon as you try to get an item from it, its always null. I.e. pm.request.url.query[0] (or .get(0)) will return null, despite the debugger showing it has something at 0.
I have no idea why, but for some reason, it is not at index 0, despite the debugger claiming it is. Instead, you need to filter the query first. Such as this:
var getParamFromQuery = function (key)
{
var x = pm.request.url.query;
var newArr = x.filter(function(item){
return item != null && item.key == key;
});
return newArr[0];
};
var getValueFromQuery = function (key)
{
return getParamFromQuery(key).value;
};
var paxid = getValueFromQuery("paxid");
getParamFromQuery returns the parameter with the fields for key, value and disabled. getValueFromQuery returns just the value.

PathJS elegant way of ignoring rest of the url

I am using path.js. I have a service like /foo/bar/baz and I use baz as parameter. So my router is like:
Path.map("#/foo/bar/:baz").to(function(){
//call the service
});
However sometimes I need a url like: /foo/bar/baz/ignore/whatever/etc/bla/bla. But /whatever/etc... part is not predictable as we may have more than one parameter.
So is there a possible way to get rest of url as a parameter after a certain level. I need something like:
Path.map("#/foo/bar/:baz(/*rest)").to(function(){
var params = this.params['rest'] ? this.params['rest'] : this.params['baz'];
});
in this case rest = ignore/whatever/etc/bla/bla

Categories

Resources