Cypress - network request with partial url - api

Im testing some API and i want to use only part of the URL.
Some of the URL params are are unexpected so when im using request i want to ignore some params.
Here is an example:
cy.request(getApi(https://api.example.com/api/geo_insights/impressions?alt_source=true&lat=40.7129032&lng=-74.0063033&targeting_radius=2500))
I want to use only the value of targeting_radius param and ignore the values of the rest of the params, like : https://api.example.com/api/geo_insights/impressions?alt_source=true&lat=**&lng=**&targeting_radius=2500.
How can i do it?

Run some string splitting on the URL
const url = `https://api.example.com/api/geo_insights/impressions?alt_source=true&lat=40.7129032&lng=-74.0063033&targeting_radius=2500`
const urlBaseAndParams = url.split('?')
const paramSection = urlBaseAndParams[1].split('&')
const params = paramSection.split('&')
const lastParam = params[params.length-1]
const requestUrl = `${urlBaseAndParams[0]}?${lastParam}`
You can merge some of those steps, I just break it down for clarity.

Related

Can not replace query string via Vue router

I have a Vue app which needs to remove query parameter from url. Url looks like http://localhost:8080/campaigns/279707824dd21366?screenshot=1 Needs to remove screenshot param without redirect. So I found solution which looks like this:
if( this.$route.query.screenshot ) {
this.screenshot();
const query = this.$route.query;
delete query.screenshot;
this.$router.replace({query: null}).catch((error)=>{
console.log(error);
});
}
This throws me an error without redirect:
NavigationDuplicated: Avoided redundant navigation to current
location: "/campaigns/279707824dd21366?screenshot=1".
I also tried this:
if( this.$route.query.screenshot ) {
this.screenshot();
const query = this.$route.query;
query.screenshot = null;
this.$router.replace({query});
}
What is wrong with this code? Thanks for any help.

How Can I use colon (:) with vue Router

My url re-updating after the push
I want to make this url:
www.abc.com/istanbul-taksim-otelleri?checkin=2019-05-08&checkout=2019-05-16&filters=meal_types:full,half,etc;stars:1,2,4
const query = {
checkin: '2019-05-08',
checkout: '2019-05-16',
filters:'meal_types:full,half,etc;stars:1,2,4'
}
this.router.push({query})
after this gettin like this
www.abc.com/istanbul-taksim-otelleri?checkin=2019-05-08&checkout=2019-05-16&filters=meal_types%3Afull,half,etc%3Bstars%3A1,2,4
do you have any idea ? how can fix it ?
See https://w3schools.com/tags/ref_urlencode.asp - %3A is just a URL-encoded colon. URL-encoding Strings is standard practice, and in most cases required in order to make a valid URL.
If you need to decode a URL, something like decodeURIComponent() could work for you, e.g.:
const uri = 'www.example.com/:";[]}'
const encodedURI = encodeURIComponent(uri)
console.log(encodedURI)
const decodedURI = decodeURIComponent(encodedURI)
console.log(decodedURI)

How to use cryptocomapre api to display price of BTC?

I'm working on an electron app and im using cryptocompare api to display BTC price but it dosen't displays. I've tried every solution i could think of, some help would be appreciated!!
const electron = require('electron');
const path = require('path');
const BrowserWindow = electron.remote.BrowserWindow;
const axios = require('axios');
const notifyBtn = document.querySelector('.notify-btn');
const price = document.querySelector('.price');
const targetPrice = document.querySelector('.target-price');
function getBTC(){
const cryptos = axios.get('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD&api_key={api_key}')
price.innerHTML = '$'+cryptos
}
getBTC();
setInterval(getBTC, 20000);
It gives me an output of '$[object Promise]'
In the documentation for axios, it says you need to do this instead:
axios.get(url)
.then(function (response) {
// do something with response
});
This is because the value returned by axios.get isn't a response, it's a promise that will resolve to a response. (So it gets coerced to the string [object Promise].) If you don't know what this means, read this link. Basically promises are a way of dealing with tasks that take a long time to run (such as api calls) without blocking other javascript code from runnning. But anyway, what you want is this:
function getBTC(){
axios.get('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD&api_key={api_key}')
.then(function(response) {
var data = response.data;
var cryptos = // get cryptos from data somehow
price.innerHTML = '$'+cryptos;
});
}
I haven't read the axios documentation in detail. I believe what you are looking for is in response.data, but I couldn't tell you any more than that. Try console.log('Response:', response); to find out how the response is structured.

Deconstruct url path with user input in react native

I'm looking to make a fetch request to an API, in my code I added a text input:
constructor(props) {
super(props)
this.state = {
UserInput: '',
}
}
<TextInput onChangeText={(UserInput) =>
this.setState({UserInput})} value={this.state.UserInput} />
I can definitely see UserInput variable if I render <Text>{this.state.UserInput}</Text> in my view, however I'm trying to use that variable to generate a dynamic url path for my api request.
The url looks like that https://api.trading.com/1.0/stock/msft/company msft is what I have to change by UserInput
In pure javascript, I usually do something like this:
const userstock = UserInput;
const path = "https://api.trading.com/1.0/stock/";
const end = "/company";
const url = path + userstock + end;
I changed var by const because it's react native but it's still not working,
Can't find variable: UserInput
I also tried https://api.trading.com/1.0/stock/${UserInput}/company can someone help on this please? Thanks
As you write,it works if you render <Text>{this.state.UserInput}</Text>!
Why not write like this:
const userstock = this.state.UserInput;
or :
"https://api.trading.com/1.0/stock/"+ this.state.UserInput +"/company"
I suggest you should read official document and learn more about props and state!

Making a REST API: How do I route a filepath using express for node

var express = require('express');
var app = express();
app.get('/files/:service/:op/:file', function(req, res){
console.log(req.params.file)
})
app.listen(3000);
Calling http://localhost:3000/files/farm/get/chicken will log chicken.
Calling http://localhost:3000/files/farm/get/barn/yard/chicken will log barn.
How can I make an Express app.VERB(path, callback) path log barn/yard/chicken?
Change your route to something like this:
app.get('/files/:service/:op/*', function(req, res){
console.dir(req.params);
console.log(req.params[0]); // this is what you want
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('OK');
});
Notice how the last parameter was changed to an * so that it takes single values (like chicken) or multi-values (like barn/yard/chicken). The issue here is that the slash character is used to split the URL components but you want to sometimes split the values but not others. The * will automatically lump the last set of values (1 or many) into a single element.
See http://expressjs.com/api.html#req.params
Try this:
console.log(req.params.service + "/" + req.params.op + "/" + req.params.file);