How to read query parameters in spray? - scala

I want to get parameters such as String and integer in spray,for example:
http://localhost:8080/nexus?taskId=1&operatorId=3&version=10&day=12&hour=7&minute=3
I use code:
path("nexus"){
get {
parameters('taskId.as[Int], 'operatorId.as[Int],'version.as[Int],'day.as[Int],'hour.as[Int] ,'minute.as[Int])
{ (taskId,operatorId,version,day,hour,minute) =>
complete{s"$taskId"}
}
}
I use this code to test:
curl http://localhost:8080/nexus?taskId=1&operatorId=3&version=10&day=12&hour=7&minute=3
But it's lead to this error:
Request is missing required query parameter 'operatorId'
and operatorId really exist!
I don't know How to do!

The code is correct. Just wrap the URL with double quotes:
curl "http://localhost:8080/nexus?taskId=1&operatorId=3&version=10&day=12&hour=7&minute=3"

Related

Axios - Sending a GET with & sign

I have a GET call (/getTag) that has a variable 'name'.
One of my users created on with a & sign. And unfortunately the GET call is now failing because it looks like this.
/getTag?name=IS&me-1234
Unfortunately my server interprets it like this because of the & sign:
{ id: 'IS', 'me-1234': '' }
Anyone experienced this before and have a way to solve it?
You should use encodeURIComponent on a variable name before passing it to axios.
You can use params key in axios
axios.get('/getTag', {params: {name: 'Is&me123'}})
You should request like:
axios.get('https://your-site.com/getTag', { params: { name: 'IS&me-1234' } });
instead of:
axios.get('https://your-site.com/getTag?name=IS&me-1234');

LOOPBACK REST API for 'LIKE'

how can i type the REST API for "LIKE" Query on LoopBack ?
according to Loopback Documentation,
i already try like this :
ProductDealers?filter={"where":{"DealerCode":"T001","Active":"true","SKU":{"like":"1000.*"}}}
but it nothing happens,
please help me?
It would be something like
Post.find({
where: {
title: {
like: 'someth.*',
options: 'i'
}
}
});
and for api calls
?filter={"where":{"title":{"like":"someth.*","options":"i"}}}
Please take a look at this PR for more info
i found the answer , i didn't know why its can't work when i used brackets " {} "
but when i used " [] " , it works very well, like
Products?filter[where][Name][like]=%25" + valFilter + "%25&filter[where][Active]=1&filter[where][Deleted]=0"
Cheers!
A bit late but i actually found this after looking for something else.
The problem for most people is, that there is something called Url Encoding.
Read more here:
https://en.wikipedia.org/wiki/Percent-encoding
So if you stringify your json filter like in the examples above, make sure you put the stringified object into an Uri Encoder which will make sure you will get what you expect and will control the encoding of your values
let t_filter = {
where: {
title: {
like: 'someth.*',
options: 'i'
}
}
};
let result = encodeURI(JSON.stringify(t_filter));
After that send the result to your Api and not only the stringified Object

Error while issuing REST put request to Google Firebase [duplicate]

I'm trying to make a POST request from Parse to Firebase, using Parse Cloud Code and Firebase's REST API.
Parse.Cloud.define("createChatRoom", function(request, response) {
Parse.Cloud.httpRequest({
url: 'https://myapp.firebaseIO.com/' + '.json',
method: 'PUT',
body: {"hi": "hello"}
}).then(function(httpResponse) {
response.success("Successfully posted hello!");
},function(httpResponse) {
response.error("failed to post hello" + httpResponse.text)
})
})
However, this code makes Firebase respond with the following error:
"Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."
I have tried a multitude of combinations for body, including variations of apostrophes, integers, and removing brackets altogether.
Any ideas?
Answering my question:
JSON for Firebase must be wrapped in single quotes ':
body: '{"hi": "hello"}'
I think it's better to use like this body: JSON.stringify({"hi": "hello"})

How to read query parameters in akka-http?

I know akka-http libraries marshal and unmarshal to class type while processing request.But now, I need to read request-parameters of GET request. I tried parameter() method and It is returning ParamDefAux type but i need those values as strings types
I check for answer at below questions.
How can I parse out get request parameters in spray-routing?
Query parameters for GET requests using Akka HTTP (formally known as Spray)
but can't do what i need.
Please tell me how can i extract query parameters from request. OR How can I extract required value from ParamDefAux
Request URL
http://host:port/path?key=authType&value=Basic345
Get method definition
val propName = parameter("key")
val propValue = parameter("value")
complete(persistanceMgr.deleteSetting(propName,propValue))
My method declarations
def deleteSetting(name:String,value:String): Future[String] = Future{
code...
}
For a request like http://host:port/path?key=authType&value=Basic345 try
path("path") {
get {
parameters('key.as[String], 'value.as[String]) { (key, value) =>
complete {
someFunction(key,value)
}
}
}
}
Even though being less explicit in the code, you can also extract all the query parameters at once from the context. You can use as follows:
// Previous part of the Akka HTTP routes ...
extract(_.request.uri.query()) { params =>
complete {
someFunction(key,value)
}
}
If you wish extract query parameters as one piece
extract(ctx => ctx.request.uri.queryString(charset = Charset.defaultCharset)) { queryParams =>
//useyourMethod()
}

Gatling Conditional on Response Body

I am currently using Gatling and I have a scenario whereby I perform a number of GET requests and depending on the body of the responses I would like to perform a different scenario.
I have this at the moment that doesn't appear to work as expected -
val repeatSpin = scenario("repeatScenario1").repeat(10) {
exec(
scenario1
)
.doIf(bodyString => bodyString.equals("<SwitchEvent/>")){
exec(scenario2)
}
}
What am I doing wrong?
It looks like you've got the .doIf parameters wrong - it either takes a key in the session and the value you expect, like:
.doIf("${bodyString}", "<SwitchEvent/>") { myChain }
Or, an Expression[Boolean] - the argument you get is the session; to get values out of a session you do something like session("bodyString").as[String]. So, passing a function to the doIf could look like
.doIf(session => session("bodyString").as[String].equals("<SwitchEvent/>")) { myChain }