Request Param truncated - angularjs

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

Related

Getting hashtags from $request in Yii2

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

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.

Trouble with Piwik API URL

I'm trying to make an application to get data from the Piwik API to process and analyse. I'm having trouble getting the API URL to work properly for our specific site (our Piwik is a multi-site installation, our site is identified by a segment variable, specifically customVariableValue1=9).
(I've replaced our domain and token with placeholders)
This URL works fine:
https://example.com/piwik/?module=API&method=Actions.getPageUrls&idSite=3&date=yesterday&period=day&format=json&token_auth=mytoken
However when I try to access this:
https://example.com/piwik/?module=API&method=Actions.getPageUrls&idSite=3&date=yesterday&period=day&%20segment=customVariableValue1%3D%3D9#&format=json&token_auth=mytoken
I get this error: <error message="You can't access this resource as it requires an 'view' access for the website id = 3."/> back. The token I am using is definitely correct so I have no idea why it's coming back with that error.
Any help would be greatly appreciated! :)
You have token_auth after # sign so it is interpreted as fragment identifier, not as part of query.
This URL should be valid:
https://example.com/piwik/?module=API&method=Actions.getPageUrls&idSite=3&date=yesterday&period=day&format=json&token_auth=mytoken&segment=customVariableValue1%3D%3D9#

Google Cloud Console - New Project's giving "invalid_client", and old projects give redirect_uri mimatch

I have been facing the same problem from Google Cloud API Console for over a day now.
I tried to add a new redirect_uri for an old project to use it in my webapp but it gives me "redirect_uri mismatch" error. Old redirect uri still work as they used to.
I even tried creating a fresh project from the start and then registered a new application. This now gives me "invalid_client" error via google oauth.
If the old redirect_uri redirect as they should, then why doesn't the new one? Does something else need to be done when addding a new redirect uri?
Your errors are all self explanatory, so you simply need to carefully retrace your steps and check typing, that you are using the correct client id with the appropriate urls.
it gives me "redirect_uri mismatch" error.
The uri must match character for character. eg. watch for http(s) and trailing slash
This now gives me "invalid_client" error via google oauth.
Either you forgot to enable the API or you haven't correctly installed the new client ID in your app.
Does something else need to be done when adding a new redirect uri?
NO. Just make sure it exactly matches the URL you are passing.
It's sometimes easier to switch back to the old API Console. There is a faint grey link at the bottom of the screen in the new cloud console.
This looks like a bug.
See Newly created Oauth Client IDs don't work for what seems like a successful workaround.

Bad Request, Your browser sent a request that this server could not understand

There are two application servers and a switch. When i access application by using application server ip it works fine. However if i use switch ip in my url Bad request error throws up only for firefox and chrome for a few links only.
Here is a detailed explanation & solution for this problem from ibm.
Problem(Abstract)
Request to HTTP Server fails with Response code 400.
Symptom
Response from the browser could be shown like this:
Bad Request
Your browser sent a request that this server could not understand.
Size of a request header field exceeds server limit.
HTTP Server Error.log shows the following message:
"request failed: error reading the headers"
Cause
This is normally caused by having a very large Cookie, so a request header field exceeded the limit set for Web Server.
Diagnosing the problem
To assist with diagnose of the problem you can add the following to the LogFormat directive in the httpd.conf:
error-note: %{error-notes}n
Resolving the problem
For server side:
Increase the value for the directive LimitRequestFieldSize in the httpd.conf:
LimitRequestFieldSize 12288 or 16384
For How to set the LimitRequestFieldSize, check Increase the value of LimitRequestFieldSize in Apache
For client side:
Clear the cache of your web browser should be fine.
If you use Apache httpd web server in version above 2.2.15-60, then it could be also because of underscore _ in hostname.
https://ma.ttias.be/apache-httpd-2-2-15-60-underscores-hostnames-now-blocked/
I just deleted my stored cookies, site data, and cache from my browser...
It worked. I'm using firefox...
Make sure you url encode all of the query params in your url.
In my case there was a space ' ' in my url, and I was making an API call using curl, and my api server was giving this error.
Means the following url
http://somedomain.com?key=some value with space
should be
http://somedomain.com/?key=some%20value%20with%20space
THIS IS CAUSED BY TOO MANY COOKIES!
To SOLVE - Chrome: go into 'developer mode' -> ctrl + shift + i
On top you will see console, network and LITTLE BUTTON THAT LOOKS LIKE ARROWS >>> click on that for APPLICATION
On Left, under STORAGE, find COOKIES.
There will be little DOWN ARROW indicating a drop down, click on this.
now you will see the website something like: www.investing.com
RIGHT CLICK IT and select Clear
Reload.
Works!
Alternatively, clear cookies and cache in a traditional way, and it will work too.
In my case is a cookie-related issue, I had many cookies with extremely big values, and that was causing the problem.
You can replicate this issue here on stackoverflow.com, just open the console and type this:
[ ...Array(5) ].forEach((i, idx) => {
document.cookie = `stackoverflow_cookie${idx}=${'a'.repeat(4000)}`;
});
What is that?
I am creating 5 cookies with a string of length or value of 4000 bytes; then reload the page and you will see the same issue.
I tried it on google.com and you'll get the error but they automatically clear the cookies for you, which is a nice fallback to start fresh.
I was testing my application with special characters & was observing the same error. After some research, turns out the % symbol was the cause. I had to modify it to the encoded representation %25. Its all fine now, thanks to the below post
https://superuser.com/questions/759959/why-does-the-percent-sign-in-a-url-cause-an-http-400-bad-request-error
I'm a bit late to the party, but bumped in to this issue whilst working with the openidc auth module.
I ended up noticing that cookies were not being cleared properly, and I had at least 10 mod_auth_openidc_state_... cookies, all of which would be sent by my browser whenever I made a request.
If this sounds familiar to you, double check your cookies!
in my case:
in header
Content-Typespacespace
or
Content-Typetab
with two space or tab
when i remove it then it worked.
in my magento2 website ,show exactly the same error when click a product,
my solution is to go to edit the value of Search Engine Optimization - URL Key of this product,
make sure that there are only alphabet,number and - in URL Key,
such as 100-washed-cotton-duvet-cover-set,
deleting all other special characters ,such as % .
I got Bad Request, Your browser sent a request that this server could not understand
when I tried to download a file to the target machine using curl.
I solved it by instead using scp to copy the file from the source machine to the
target machine.
If you are getting this error on the WordPress website, check the below solution.
Corrupted Browser Cache & Cookies: Delete your Cookies and clear your cache
Restart your server
For GET Request make sure that passing parameters are url encoded.
if you are using php you can use urlencode function
If you have this same problem and none of the other solutions worked, please check again the url.
In my case it was a space in the end, when it was added to the Cronjob, someone also copied a blank space by accident.
check your data types are correct or not.
for ex: if you send the file, you need to consider to send the full object of the file

Resources