Routing in NancyFX - nancy

A sample route in Nancy can be like:
Get["/method/key1={value1}/key2={value2}"]
which can be reached by calling it as:
/method/key1=foo/key2=bar
I want to write the querystring in below manner:
method?key1=value1&key2=value2
What would be the route for this?

It will be the following route:
GET["/method"] => x
{
var key1 = Request.Query.key1;
var key2 = Request.Query.key2;
};
Where Query is DynamicDictionary.
Related Questions:
Get url parameters in NancyFx
Why are no query parameters being passed to my NancyFX module?
NancyFX: How do I check if query-string / form values have been correctly passed to my handler?

Related

return value is always None in google-app-engine script

I am using the following code to parse url in google-app-engine script:
from urlparse import urlparse, parse_qs
def parse_url(url):
parsed_url = urlparse(url)
params = parse_qs(parsed_url.query)
return params
class Handler(webapp2.RequestHandler):
def get(self):
url = self.request.path_info
params = parse_url(url)
self.response.write(params)
Params always None after calling the function.
However, when using the exact same parsing code inside the handler (not as a function) - the parsing is good and I get a non-empty dictionary in params.
Any idea why it could happen?
The self.request.path_info value is not the full URL you need to be passing down to urlparse() to properly extract the params, it has the query parameters stripped off, which is why you get no parameters. It doesn't work inside the handler either, you may have done some additional changes since you tried that.
To get the parameters using your parse_url() pass the full url:
url = self.request.url
params = parse_url(url)
But you should note that all this is rather unnecessary, webapp2 already has a parameter parser included, except it returns a MultiDict. From Request data:
params
A dictionary-like object combining the variables GET and POST.
All you have to do is convert it to a real dict identical to the one your parse_url() produces:
self.response.write(dict(self.request.params))

react-router-dom with multiple query strings does not work

I am new to react router. I use react-router-dom 4.2.2
in my router set up I have:
<Route path={"/confirmuser/:confirmation_code/:username"} component={ConfirmUser} />
and here is a sample url I am trying to achieve:
localhost:3003/confirmuser?confirmation_code=986797&username=5f1
As you see I am trying to send multiple query strings.
in the confirmUser I read the query strings as follow:
console.log(this.props.match.params.confirmation_code);
console.log(this.props.match.params.username);
However I do not even get directed to this component and it seems react is not able to route to that page properly.
Any idea?
React-router v4 doesn't parse query strings anymore, so you either have to do the parsing yourself (not recommended), or use a package like query-string. An easy way to access the values is with a wrapper component, like this:
import * as queryString from 'query-string';
..
const WrappedConfirmUser = () => {
const {confirmation_code, username} = queryString.parse(location.search);
return <ConfirmUser confirmation_code={confirmation_code} username={username}/>;
}
You are trying to map search-params to path segments?
The Route you defined will try to match the path, not the search params.
Try:
http://localhost:3003/confirmuser/986797/5f1
and the values will be in this.props.match.params like this:
{
confirmation_code: '986797',
username: '5f1',
}
if you still want to read the search params, you can access them from this.props.location.search, but react-router will not match them to a route for you.
Your path doesn't match your url.
It matches localhost:3003/confirmuser/986797/5f1
Then you can access params using extra prop match:
{props.match.params.confirmation_code}
{props.match.params.username}

Get location path without parmaters

Is there a way with AngularJS to and get a URL path minus all parameters? I don't want to grab it out of the address bar, I have a var where the value is a URL that I want the path out of.
Turn this: var url1 = http://domain.com/dir1/dir2/?param1=123&param2=456
Into this: /dir1/dir2/
This just wants to return the path with the parameters:
var justPathLocation = $location.path();
window.location.pathname returns exactly what you need.
On this URL:
http: //stackoverflow.com/questions/30385367/get-location-path-without-parmaters/30386013#30386013?param=value
it returns this:
"/questions/30385367/get-location-path-without-parmaters/30386013"
So it returns only the path name, without query strings or fragment identifiers.
UPDATE
Here's a regex way, it uses the domain name, which you can hard-code or fetch using window.location.hostname:
var url = "http://domain.com/dir1/dir2/?param1=123&param2=456";
alert(url.match(/http:\/\/domain\.com(.*)\?/)[1]);
And a more generic approach - ^.*\/\/.*?(\/.+)\?:
var url = "http://domain.com/dir1/dir2/?param1=123&param2=456";
alert(url.match(/^.*\/\/.*?(\/.+)\?/)[1]);

AngularJS | Set Path Parmeter while using $http.get method

I have a GET endpoint with URI as /user/user-id . The 'user-id' is the path variable here.
How can I set the path variable while making the GET request?
This is what I tried:-
$http.get('/user/:id',{
params: {id:key}
});
Instead of replacing the path variable, the id get appended as query param.
i.e my debugger show the request URL as 'http://localhost:8080/user/:id?id=test'
My expected resolved URL should be like 'http://localhost:8080/user/test'
$http's params object is meant for query strings, so key-value pairs you pass into params are output as query string keys and values.
$http.get('/user', {
params: { id: "test" }
});
Becomes: http://localhost:8080/user?id=test
If you need http://localhost:8080/user/test, you can either:
Construct the url yourself,
$http.get('/user/' + id);
Or, use $resource (specifically $resource.get https://docs.angularjs.org/api/ngResource/service/$resource). This is a little cleaner.
Why not something like this?:
var path = 'test';
$http.get('/user/' + path, {});

Get resource parameters from url in angularjs

In angularjs, Given a url http://example.com/photos/123 and url template http://example.com/photos/:id (that is used in $resource). How to get the hash: {id: 123}?
All url parameters can be accessed with the $routeParams service:
From the $routeParams documentation:
The route parameters are a combination of $location's search() and path(). The path parameters are extracted when the $route path is matched.
Example:
// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:1, sectionId:2, search:'moby'}
EDIT: Given the below comments, I extend my answer
// The example url you provided.
var url = 'http://example.com/photos/123';
// We get a list with all the parts of the url.
var partList = url.split(/\/|\?|&|=|\./g);
// We get the bit that we know is the photo's id.
var idPart = partList[5];
You would use Angular $routeParams.
For example in your controller:
$routeParams.id
would return '123'
Edit: As pointed out by okigan the default routing assumes you have a location hash so your route would need to be http://example.com/#/photos/123. However you can turn this off to retain your desired routing by turning on html5mode. Please see removing #
Update: You can also access the url parts using regular Javascript. The following would get the second url part.
id = (window.location.pathname).split('/')[2]
You could use this as a base to extend the functionality.

Resources