Can't read response data and headers in an AngularJS interceptor - angularjs

I have an API in my server that sends a custom header (lets name it "customHeader") that responds to http://localhost:8081/my/test/api.
I'm trying to read that custom response header from an interceptor in angularJS:
angular.module('oauth.interceptor', []).factory('readResponseHeader', ['$q',
function ($q) {
return {
response': function(response) {
console.log("response: %o", JSON.stringify(response));
console.log("headers: %o", response.headers());
return response;
}
}
}
]);
But all I get are empty data and headers in response. The console log shows the following json as response:
{
"data": [],
"status": 200,
"config": {
"method": "POST",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "http://localhost:8081/my/test/api",
"data": {
"example": "request data"
},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
"Authorization": "Bearer XXXXXXXXX..."
},
"cached": false
},
"statusText": "OK"
}
And the following as headers:
{
"content-type": "application/json; charset=UTF-8"
}
I expected my customHeader to be there, and be able to read it with something like:
response.headers().customHeader
But it clearly isn't there (in fact, many other headers are missing!).
How am I supposed to read it?

I finally found the problem wasn't really in the AngularJS part, but on the server side part.
The point is that, to be able to access the header, the server must add the Access-Control-Expose-Headers header containing the name of the custom header, something like this:
Access-Control-Expose-Headers: customHeader

Related

I want to send the meeting invite using angularjs. But facing some issues

I am using the rest api of outlook for sending the request. But when i am calling this with above code, in console i am getting error
unhandled exception
$scope.createEvents = function(){
alert("here");
var url = "https://outlook.office.com/api/v2.0/$metadata#Me/Calendars";
$scope.token = "AQABAAIAAADX8GCi6Js6SK82TsD2Pb7rFu9WBTIi5a6r4Up0vk8NCmPR719K3Uiz7NPUBoT9RwewzSWjPfUIybYn9fVVF2dhQ8b0ObN3oIv5Tq91GwZiTuyBhWZ_s07uDJpCUh40K4Bn2F5eEP9TAp8-5eMY0hfyXb4vIukQwTAe9yXCG75WUS08M7m-_kFbtx-TVq-Y2-SKh8Ut7-v4UQq4NYhlf5LQC1arNbwAZVndfND1vSNGcs1BVJboWd7bcgohHecaR57cAuFav2vfsVEm8n3_IKnlapHzWsyXOw7gXnxTmH2pkfAie0LCiQv8C8nQRnYnLquaWKg6b_ZzTl4ela0EwC9cN74BVMQHFWm6NY1EVM8s-HYevrT8R-WwkAUJXprP40Jp-weLY4-K7vOGk0N0n6fQIeu_WoORIZ18-oKxda6j4XunmrLbWEapQy-Oms9BIAs-AZIVN6Ph1zxQk3CC1bbGK6QkiiL4c2Sgrx_6YoxZt1cMh36pjhbe_TxpC2alk5zJineTK0AZUdGJQWGoy-9fTwtzTbYiaRaV4rispV-q-yiYzpWQ1UWROIPZ5qDq2jlCV7ovTDCWhNd_JgKzzHf-2wANBun9WcX924UVcLkkfvh6XU-QxCo1N6gfGYGUwNNJA1kTZjSyxgKSOAKGxKXz96r5qbtJp34Ci9lEO-PpSeLAL5gHoUOX_PlbM_FO4mLcNEu_fB0Us-5sbV8pKPU-WnfLq3PI8gK3lCi5D_itjesuex1f-o1d1vJ3M4jvx-sgAA";
var add_events = {
"Subject": "Discuss the Calendar REST API",
"Body": {
"ContentType": "HTML",
"Content": "I think it will meet our requirements!"
},
"Start": {
"DateTime": "2016-10-10T18:00:00",
"TimeZone": "Pacific Standard Time"
},
"End": {
"DateTime": "2016-10-10T19:00:00",
"TimeZone": "Pacific Standard Time"
},
"Attendees": [
{
"EmailAddress": {
"Address": "nishanth.singh#subex.com",
"Name": "Nishanth Kumar Singh"
},
"Type": "Required"
}
]
};
$http({
method: 'JSOPN',
url: url,
headers:{
'Authorization':'Bearer '+$scope.token,
'Content-Type': "application/json",
'Accept': 'application/json;odata.metadata=minimal',
'Access-Control-Allow-Origin':'*'
},
data: add_events
}).Succes(function (response) {
alert("Saved")
});
}
Replace
}).Succes(function (response) {
with
}).success(function (response) {
You have used upper case Success in your code & the spelling is incorrect as well. Let me know once you made this change.
Try to use .then() rather than success() as success() has been deprecated.
Update 1
Since you are using version 1.6, use .then(). success() is deprecated as I said
$http({
method: 'JSOPN',
url: url,
headers:{
'Authorization':'Bearer '+$scope.token,
'Content-Type': "application/json",
'Accept': 'application/json;odata.metadata=minimal',
'Access-Control-Allow-Origin':'*'
},
data: add_events
}).then(function (response) {
alert("Saved")
});
Update 2
You are now facing CORS issue. for quick fix try chrome plugin.
Get proper header to complete the request or you need to config the server to handle such requests.
change method: 'JSOPN' to
method: 'JSON'

How do I parse out the file name in a form-data trigger

From Postman I am doing a Post to my Logic App. I am sending the following:
{
"headers": {
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Accept": "*/*",
"Accept-Encoding": "gzip,deflate",
"Host": "prod-24.centralus.logic.azure.com:443",
"User-Agent": "PostmanRuntime/6.4.0",
"Postman-Token": "19018057-41ef-4f96-a3a1-cdbf0a1918bc",
"Content-Length": "486",
"Content-Type": "multipart/form-data; boundary=--------------------------117388521639837767242570"
},
"body": {
"$content-type": "multipart/form-data; boundary=--------------------------117388521639837767242570",
"$content": "LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLTExNzM4ODUyMTYzOTgzNzc2NzI0MjU3MA0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJzb2Z0d2FyZSI7IGZpbGVuYW1lPSJmaWxlLnppcCINCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vemlwDQoNClBLAwQUAAAACACZVEhLillatwgAAAAGAAAACAAAAGZpbGUudHh0K0ktLslNBQBQSwECHwAUAAAACACZVEhLillatwgAAAAGAAAACAAkAAAAAAAAACAAAAAAAAAAZmlsZS50eHQKACAAAAAAAAEAGADAJqtCS0DTATYTq0JLQNMBlyUeQktA0wFQSwUGAAAAAAEAAQBaAAAALgAAAAAADQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tMTE3Mzg4NTIxNjM5ODM3NzY3MjQyNTcwDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImRldmljZUlkIg0KDQpzMTIzNDUNCi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0xMTczODg1MjE2Mzk4Mzc3NjcyNDI1NzAtLQ0K",
"$multipart": [
{
"headers": {
"Content-Disposition": "form-data; name=\"software\"; filename=\"file.zip\"",
"Content-Type": "application/zip"
},
"body": {
"$content-type": "application/zip",
"$content": "UEsDBBQAAAAIAJlUSEuKWVq3CAAAAAYAAAAIAAAAZmlsZS50eHQrSS0uyU0FAFBLAQIfABQAAAAIAJlUSEuKWVq3CAAAAAYAAAAIACQAAAAAAAAAIAAAAAAAAABmaWxlLnR4dAoAIAAAAAAAAQAYAMAmq0JLQNMBNhOrQktA0wGXJR5CS0DTAVBLBQYAAAAAAQABAFoAAAAuAAAAAAA="
}
},
{
"headers": {
"Content-Disposition": "form-data; name=\"deviceId\""
},
"body": {
"$content-type": "application/octet-stream",
"$content": "czEyMzQ1"
}
}
]
}
}
I am trying to get filename value and the content of the file.
I will be creating a Blob file
Thanks,
Here's how I'm doing it. I'm assuming you're using the HTTP trigger set to POST. After the HTTP trigger add a Foreach connector, so you can loop through all the files if you're doing a multi file upload.
The Foreach source will be this expression:
triggerOutputs().body['$multipart']
Within the foreach you can parse the filename with this ugly expression, which is pretty bad but it works:
replace(replace(item().headers['Content-Disposition'], 'form-data; name="myFileUploadInput"; filename="', ''), '"', '')
AFAIK there's no regex support in expressions, which would've been better. You could probably do something with substring, but it'd probably be pretty ugly too.
Here's how you'd get the body within the foreach to save to blob storage: item().body

Outlook Rest calling form angularjs

Using outlook I am trying to create event, When i send request using POSTMAN its working fine, But same code in Angularjs its not wotking.
what is wrong with code.
Please help.
$scope.createEvents = function(){
var url = "https://outlook.office.com/api/v2.0/$metadata#Me/Calendars";
//var url = "https://outlook.office.com/api/v2.0/$metadata#me/Calendars";
var add_events = {
"Subject": "Discuss the Calendar REST API",
"Body": {
"ContentType": "HTML",
"Content": "I think it will meet our requirements!"
},
"Start": {
"DateTime": "2016-10-10T18:00:00",
"TimeZone": "Pacific Standard Time"
},
"End": {
"DateTime": "2016-10-10T19:00:00",
"TimeZone": "Pacific Standard Time"
},
"Attendees": [
{
"EmailAddress": {
"Address": "sathish.gopikrishnan#stradegi.com",
"Name": "Sathish Gopi"
},
"Type": "Required"
}
]
};
$http({
method: 'POST',
url: url,
headers:{
'Authorization':'Bearer '+$scope.token,
'Content-Type': "application/json",
'Accept': 'application/json;odata.metadata=minimal',
'Access-Control-Allow-Origin':'*'
},
data: add_events
}).Succes(function (response) {
alert("Saved")
});
I am getting.
Failed to load resource: the server responded with a status of 406 (Not Acceptable). To solve this problem i am using this code now
$scope.createEvents = function(){
var url = "https://outlook.office.com/api/v2.0/$metadata#Me/Calendars";
//var url = "https://outlook.office.com/api/v2.0/$metadata#me/Calendars";
var add_events = {
"Subject": "Discuss the Calendar REST API",
"Body": {
"ContentType": "HTML",
"Content": "I think it will meet our requirements!"
},
"Start": {
"DateTime": "2016-10-10T18:00:00",
"TimeZone": "Pacific Standard Time"
},
"End": {
"DateTime": "2016-10-10T19:00:00",
"TimeZone": "Pacific Standard Time"
},
"Attendees": [
{
"EmailAddress": {
"Address": "sathish.gopikrishnan#stradegi.com",
"Name": "Sathish Gopi"
},
"Type": "Required"
}
]
};
$http({
method: 'JSONP',
url: url,
headers:{
'Authorization':'Bearer '+$scope.token,
'Content-Type': "application/json",
'Accept': 'application/json;odata.metadata=minimal',
'Access-Control-Allow-Origin':'*'
},
data: add_events
}).Succes(function (response) {
alert("Saved")
});
After Using Jsonp as method I am getting this error
Uncaught SyntaxError: Unexpected token <
406 Not Acceptable is defined as the following:
The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
see restapitutorial: http status codes
You send the following Accept header:
'Accept': 'application/json;odata.metadata=minimal'
The server says, it cannot produce this content type. If you got it running using postman, have a look at which accept header you've provided there.
Concerning your second try: jsonp is not an http method/verb (GET, PUT, POST, DELETE, PATCH). I'm not an angularjs expert but in case you need jsonp, there is a method you can call.
JSONP is a quite specific (and somewhat problematic) technique. So as long as you can do that, try to avoid it.
i know its an old post, i've had the same problem and this is the solution if someone falls into the same problem,
you should use https://graph.microsoft.com/v1.0/me/calendar/events as URL
Exemple tested code :
var url = "https://graph.microsoft.com/v1.0/me/calendar/events";
var add_events = {
"Subject": "Title",
"Body": {
"ContentType": "HTML",
"Content": " Exemple"
},
"Start": {
"DateTime": "2010-05-17T18:00:00",
"TimeZone": "UTC"
},
"End": {
"DateTime": "2010-05-17T19:00:00",
"TimeZone": "UTC"
}
};
$http({
method: 'POST',
url: url,
headers:{
'Authorization':'Bearer '+localStorage.getItem('Your Token here'),
'Content-Type':'application/json',
'Accept': 'application/json;odata.metadata=minimal',
'Access-Control-Allow-Origin':'*',
'token_type':'Bearer'
},
data: add_events
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
localStorage.setItem('etat_mytoken', 0);
$window.location.href = url_base+'/'+url_base_v+'/app/assets/views/token.html';
});

Local elasticsearch host blocking request with enabled request header from angularJS

I am trying to post a request to a local elasticsearch.
I have configured the elasticsearch.yml to include:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"
I have also tried:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: X-Requested-With, Content-Type, Content-Length, Authorization
When posting a from Postman there are no issues, but when posting from AngularJS I recieve this error:
XMLHttpRequest cannot load http://localhost:9200/_search/test_index/_search?size=50. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
The angular code is the following:
$http.post( vm.elasticsearchUrl, query );
var query = {
index: 'test_index',
size: 50,
body: {
"query": {
"filtered": {
"query": {
"wildcard": {
"title": "*" + searchTerm + "*"
}
},
"filter": {
"not": {
"filter": {
"terms": {
"id": [ 1, 12 ]
}
}
}
}
}
}
}
};
-------ANSWER---------
I resolved it by setting Content-Type to undefined, this then removes Content-Type from the request headers.
var promise = $http.post( vm.elasticsearchUrl, query, {
headers: {
'Content-Type': undefined
}
} );
I am not sure if this is the correct way of doing it, so if anyone has any critique I would love to hear it.

how to set headers or authentication in azure logic app http action

I'm using the basic azure http action step and see a box for authentication and header in the configuration, but no documentation on how to use it or set it. What's the syntax?
I wasn't able to use the box, but if you go to code view you can do the following:
Add the HTTP module
Click Code View
Go to Actions and modify headers:
"actions": {
"http": {
"type": "Http",
"inputs": {
"method": "POST",
"uri": "your URI",
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic *your base64 password*",
"other header": "other value"
}
},
"conditions": []
}
},

Resources