Get request to Json does it need jsonp - angularjs

I am using Angular v1.2.15 to GET a URL to retrieve JSON results.
I need to send a custom AUTHENTICATION Header with the request.
the url will send back Json.
So far I can only get both $resource and $http to even send the request
if I use JSONP type of request. However when I do this it does not send
my CUSTOM HEADER. Can I retrieve JSON content using a normal GET request?

The problem was the Ripple Emulator for Cordova apps was running the proxy on a different port. The the emulator was actually not allowing the http request.

Related

Angularjs 1.8: extract header info from an URL or a GET request

How to extract header info from URL GET request?
My web app is using angularjs 1.8.
It currently has an SSO link with the token exposed since an URL request is a GET request. This link is called by 3rd party app to access my web app. The 3rd party may come from a web app on a browser or mobile app.
for example: www.example.com\login.html?accessToken=testingXYZ. With this I know I can get the token via $location and extract the path parameter.
To improve the security, I thought of putting this access token value into the request header. However I unable to extract the header info from the caller request.
I need to extract the request header info from the angularjs controller for authentication.
Here is the setup I attempted:
I use Requestly plugin on chrome to inject the header, say myToken=XYZ, to every request I sent via Chrome.
Then I use console.log($http.defaults.headers) to print out any headers from the angularjs controller.
but "myToken" is undefined.
NOTE: I believe this is a request header, not the response header.
Please help!

URLs are proxied by react-scripts only when they are called via curl or axios

I found an interesting case in one of the react-scripts apps where the proxy is configured to localhost:3001 (the front end is running on localhost:3000).
From the react stuff we make a request via axios to localhost:3000/api/results and that loads a bunch of JSON information, but if I open localhost:3000/api/results in a new browser tab that does not display the JSON but loads the HTML instead.
Why is that happening?
The real problem is that we have endpoint to download files from, like:localhost:3001/api/downloads/csv/file.csv, but they won't work, because when localhost:3000/api/downloads/csv/file.csv is not proxied to localhost:3001/api/downloads/csv/file.csv and we simply cannot call this via axios because it should be a direct call from the browser.
However, the strange thing is why does it work via axios and curl?
By doing curl localhost:3000/api/downloads/csv/file.csv (or 3001), we get the right content back.
If you are using react-scripts, then this is facilitated by Service Workers. Quoting from Service Workers - By Ankita Masand...
Service Worker acts as a proxy server that intercepts the network requests sent by your web application to the server. In the sense, requests to fetch Javascript or CSS files, images go through service worker to the server. Service Worker has the ability to modify this request or send a custom response back to the client.
Here, Service Workers are acting as a Client Side Proxy and tapping to your HTTP Requests and you get the right content back.

Node-Red flow deploy using http request

I am referring this example https://github.com/watson-developer-cloud/node-red-labs/tree/master/starter-kits/ok_watson and could successfully deploy it in Node-Red. But in this sample the input is Inject based. Is there a way of deploying this same example onto a page using HTTP request or maybe Websocket? I figured we can use http request and response for that, but how should the template code look like if i need microphone and audio out in my http webpage. Thanks in advance.
Yes it is. The flows require a text input, which could easily come from a HTTP form. So add a HTTP Input, HTTP Response and Template with a form, and you should have your text input.

Get a "Response header" Cookie from angular $http POST request

I'm writing a small app using angular.
I need to access a couch database. I only have a user in that DB.
Using cURL commands I can request a session cookie to that database and that use that cookie to request a specific document.
When in angular code I use:
$http.post(url, data).then(...);
Where 'data' has username and password, I can see the cookie using chrome (like in this image chrome dev tool output), but in a code, I can't access it.
Can someone help me with this?
You can not access an HttpOnly cookie using script in browser due to security reasons. It can only be accessed on server.
If you need to include cookies in cross domain requests use withCredentials in the request. See $http docs
$http.post returns a response with a property:
headers – {function([headerName])} – Header getter function.
So you should be able to do something like
.then((response) => {
console.log(response.headers('set-cookie'));
});
See the documentation here
Try to use response.headers('set-cookie')
For the crossdomain situation, the server has to send
Access-Control-Expose-Headers: set-cookie header to make the custom headers visible.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Expose-Headers

AngularJS - Why my request is not sending the json object to the server?

I am using $http for sending post request with json object to the nodeJS server but when I print the request params on the server side then it shows empty.Here is the code.
$http.post('/login',userObj);
This is how I handled request on server
server.post(/login/,function(req,res){
console.log(req.body);
});
The problem is not in your AngularJS code. You need to use body-parser in Nodejs to handle the request.

Resources