Headers in HTTP.get on client is not working

Hey

I’m trying to use HTTP.get with a header set on the client, this is not working (the call gets made, but without the header, resulting in a 401 response), but if I try it on the server it works fine. But I need to run it on the client.

My client code looks like this:

var baseUrl = 'http://maks-2014:8989/api/';
var options = {
	headers: {
		'X-Api-Key': 'b326f5769baa46f0b97b04364115e856'
	}
}
HTTP.get(baseUrl + 'profile', options, function(error, result) {
	console.log(result)
});

and my server code for testing looks like this:

var baseUrl = 'http://maks-2014:8989/api/';
var options = {
	headers: {
		'X-Api-Key': 'b326f5769baa46f0b97b04364115e856'
	}
}
var result = HTTP.get(baseUrl + 'profile', options);
console.log(result)

Can someone tell me why this doesn’t work?

cause options variable is not set when you exec that HTTP.get, cause it does not wait for var options = …
client side = async code = execute order kinda does not matter
at least that would be my expectation

Yes, it is. A straight assignment is synchronous, even on the client.

@benjick: 401 is unauthorised, but I’m wondering if the status code is being set correctly in the API you are getting from. I have seen CORS issues with client-side gets, which work fine from the server. Could it be that?

CORS should be alright, I tried a chrome plugin called “modify headers” and set the headers in that extension, then all queries works fine so I’m just assuming the headers aren’t actually being sent

The source for http on the client looks fine, so either the 401 is the wrong error, or it’s correct and you need to provide a WWW-Authentication header. It could be that your server’s IP is whitelisted and gets through.

Hi, I Know this from 2015, I am encountering the similar problem. Did you find a solution?