Getting hashtags from $request in Yii2 - request

I need to get hashtags from a GET-request, like this:
/v1/search/search#tag?query=word
or /v1/search/search?query=word#tag
I try to get query string:
Yii::$app->request->getQueryString()
But in first case it totally ignores query param, whereas in the second case it finds query param, but ignores hashtag. Is any way to get the hashtag from request?

You cannot get URI fragment value (hashtag) on the server (directly).
This is not a Yii limitation, the anchor # part of the url is meant to be used on the client side, and it isn't sent to the server.
According to the Wikipedia page for URI fragment:
Fragments depend on the document MIME type and are evaluated by the client (web browser).
Clients are not supposed to send URI fragments to servers when they retrieve a document, and without help from a local application (see below) fragments do not participate in HTTP redirections.
You can check by yourself looking at the network traffic, your server logs, Yii debugger logs, or at your browser console, for example, if you try to browse https://example.com/user/123#1234 the server will only get https://example.com/user/123.
You could bypass this limitation converting the values to parameters before you send the request.
// you could update
https://example.com/user/123#1234
// to
https://example.com/user/123?tag=1234

Related

Getting all entries from a database that start with letters using HTTP request

I am writing a simple filter function that sends a request and gets all entries that partially match the query. Desired behavior is if do a GET request localhost:3000/employees?email=mai I would like to receive all entries from a database starting with mai. Is there any way to construct an http requests for the desired behavior from a front-end part of the application? Perhaps some headers that tell the server that the search should not be strict? I have tryed googling it but failed. I am using AngularJS fro front-end and json-server as a back-end mock server
Up to my understanding if you filter data in front End you will get only that data filtered which you are showing. In the other hand If you do this using Query(Back End) all the data get filtered in DB.

Data Flow with React/Redux and Express

I have a React/Redux front-end with an Express back-end application, and I'm rather new, but I have a question regarding how to deal with the flow of data.
So, on my front end side, I have a search bar. When a user enters a search term, I sent a post request from React which is handled in my Express routes.js file. In this file, I am taking that search term, and I am looking for that term in my Mongo database. After that, all I want to do is send an object back if the term was found in the database.
I have used axios in this application to make an HTTP request to a certain route to pull off some data, but that was within an app.get(...) on the express side, and I used an axios.get(...) on the React side to retrieve the information.
But, this situation is slightly different since the data is flowing in two directions. Initially, from a front end to the backend, and then back-end to front-end. And in this case, I'm using app.post(...).
Now my question is, how would I retrieve the data to the front end? Could I simply just do an axios.get(...) on an app.post(...) or is there some other way to do this?
If you GET from the browser to your back-end's route which is implemented to respond to POST only, you will probably get a 405 error. Implement a POST Axios request and a POST Express reply.
You can use either GET or POST, but you need to be consistent on the server and the client side. If you do an http GET from the client, the server will only respond if you have a app.get(...) as a server route.
As far as the flow of data is concerned, both a get and a post can return data, it just needs to be specified on the express route.
After the business logic of looking if the key exists in mongo do something like res.send({'found': true}) or res.json({'found': false}). This will ensure that the data gets back to the client.
If I were to do this, I would:
1.) Use an Axios get request and pass in as a parameter the identifying attributes, such as a related _id or key phrase.
2.) Use mongoDB's query filter search parameters to index and aggregate the schema data in the DB. I would probably use .findOne or .find.
3.) Use the router callback to pass in the filtered data, then dispatch a function to save it to a state.
This way you can set up specific terms or keywords to search with, and utilize the searched data throughout the app.

HTTP POST v/s HTTP GET

I want to make a database query from frontend (Angular) to backend. But I need to send lots of parameters for that.
As far as I understand, if we are not making any database changes, it is better to use GET as it uses cached entries. POST should be used used if we need to make changes on server/DB.
But if I want to send many parameters (some are serialized objects) and make no server side changes, will it be alright to use POST request in that case and embed all parameters in the POST body instead of sending a huge URL encoded GET request?
To first clear this up: responses to POST requests can be cached, as long as the origin server returns the proper caching response headers. However, browsers and proxy servers generally don't apply caching to POST requests.
That being said, with the proper encoding you can store a lot of information in the ~ 2 KB of a query string, so GET should be the way to go.
If you're certain you'll go beyond the limits of a GET request and you'll need to go the POST way while remaining cacheable, you could introduce a kind of "nonce", for example using a hash of the search parameters:
Client does a POST to /search, with the search parameters.
Server stores the parameters somewhere, for example in a database.
Server generates a nonce from the parameters, for example by hashing the search parameters, or the row ID.
Server redirects the client to the result page for that nonce: /search/123abc.
Client requests the /search/123abc search results page.
Server performs the search based on the nonce, and returns a response which is cacheable.
This will introduce one additional HTTP roundtrip, but enable caching cross-browser and through proxy servers.
I think you should use post in this situation which is more manageable and looks clean. For more benefit of post follow these links:
Link 1
Link 2

Request Param truncated

I am just learning to build webapps using express, angular and mongoose.
My angular controller passes on the url "/contact/54a153243aac91fc28605b0b" The last bit being the _id of the record in my mongo database.
on the server side, I prepend the url with /api, so that the effective url is "/api/contact/54a153243aac91fc28605b0b"
When I am accessing the parameter on the server side, I expect to get "54a153243aac91fc28605b0b" as req.param.id, but i am just getting "54".
Interestingly if I type in the following on the address bar of the browser directly, I am able to get the full ID. I.e. if I type "http://localhost:3000/api/contact/54a153243aac91fc28605b0b" In this case, I am also getting the expected JSON response.
I am struggling to debug this. Any hints. Please help.
In express app.use method does not prepend anything to the url. It is used to apply middleware for appropriate url.
So, if you have app.use('/api',api), then you should always access it with the url that starts with /api.
That's why your angular code is not working as expected. You should have the full url including the /api part in it.
I found the reason why the param was truncated. IN a certain place in my code I was doing a 'parseInt($routeParams.id, 10)' which was causing the Hex id to be truncated to a decimal.
Than you for the support!
~Kishan

Rest web service consumes XML with other parameters as well

Would it be possible to consume an XML request along with other parameters as well?
These params are not Form Params.
The client would pass these params with XML.
Actually when you talk about REST web service, you talk in terms of GET, POST, DELETE and PUT.
If you talk about the client only then POST and PUT will allow you to pass the user object, where as others will may ask for the id to perform the specific operation.
So, in a web service you have the parameter, ID, and it could be pathparam/formparam/queryparam and you have a #consume tag as well. But I have never seen a client that would pass these parameters with XML, because the URL path is not affected.

Resources