Calling rest api from meteor app

Hi there, how can i call a rest api with authentication from meteor, appreciate if you can guide to an example, i have added the http package to the meteor project. using advanced rest client chrome app, i am able to get the data by passing the following URL and 2 headers. this app is using the already logged in browser session.

URL: https://xxxxx:9443/ccm/oslc/contexts/_ZoC0QNS3EeGPlow7Bl9IAw/workitems
headers: Accept", “application/json"
OSLC-Core-Version”, “2.0”

How can i pass the headers and user name password of the rest api service.

No guarantee, but you could try it like this:

var response = HTTP.get(‘http://blahblah’, {
params: {username: ‘yourvalue’, password: ‘yourvalue’}
});

this is not working. thanks

Calling rest api is very simple in meteor, more info http://docs.meteor.com/#/full/http_call

HTTP.call("GET", "http://api.twitter.com", {
    headers: {
    	"Accept": "application/json",
    	"OSLC-Core-Version": "2.0",
    	...
    }
});

thanks @mrphu3074, tried this, but getting the following error, as we are not passing the username/password, does that mean its using the browser session? Any idea?

Error: SELF_SIGNED_CERT_IN_CHAIN

why apis use browser session?

if that is possible then i can make meteor app use the already logged in browser session and get the required data? if meteor can authenticate using user name and password, then i dont need the browser session usage. Any idea how to pass the username and password and maintain the session in meteor so that subsequent api calls use that session.

actually i want to call this rest api in my meteor app. Does meteor support this?

https://jazz.net/jazz/oslc/contexts/_Q2fMII8EEd2Q-OW8dr3S5w/workitems

What I’m describing below is not Meteor-specific. Neither is the error message SELF_SIGNED_CERT…

It basically means your HTTPS URL is not signed with a ‘certified’ (bought) certificate :innocent:

As the name suggests, it is ‘self-signed’. You need to add the certificate to your server’s certificate pool, or find out how to bypass certificae validation (have no idea how to do it in Meteor). Probably it’s written in the official documentation? :innocent:

They have a forum there: https://jazz.net/forum

But in general, REST api does not have to depend on anything. It’s all headers and parameters, e.g. query string. Possibly, this one needs a token authentication of some sort, but without seeing the documentation it’s hard to advise on anything.

This forum support this way:

var result = HTTP.call('GET', 'https://jazz.net/action/profile/edit', {
	npmRequestOptions: {
		auth: {
			user: 'username',
			pass: 'password'
		}
	}
});

@mrphu3074:i have tried this way but dint help, i found on the jazz forum someone has achieved using the XHR, the following is the code, how can i map it to meteor package methods.

 authorize : function(callback) {
	contentComment.showLoading();
	var xhr = contentComment.createXMLHttpRequest();
	xhr.open('POST', contentComment.OSLC_CONTENT_TYPES.SERVICE_URL
			+ '/authenticated/identity', false);// must visit this url first
	xhr.onload = function(e) {
		if (xhr.readyState == 4 && xhr.status == 200) {
			xhrX = contentComment.createXMLHttpRequest();
			xhrX.open('POST', contentComment.OSLC_CONTENT_TYPES.SERVICE_URL
					+ '/authenticated/j_security_check', false);
			xhrX.setRequestHeader('Content-Type',
					'application/x-www-form-urlencoded;charset=UTF-8');
			xhrX.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			xhrX.setRequestHeader("Cache-Control",
					"no-cache,no-store,must-revalidate");
			xhrX.setRequestHeader("Pragma", "no-cache");
			xhrX.setRequestHeader("Expires", "-1");
			// xhr.setRequestHeader("X-jazz-downstream-auth-client-level","4.0");
			xhrX.onload = function(e) {
				if (xhrX.readyState == 4 && xhrX.status == 200) {
					// log
					// on
					// success
					authrequired = xhrX
							.getResponseHeader("X-com-ibm-team-repository-web-auth-msg");
					if (authrequired == "authrequired") {
						
					} else if (authrequired == "authfailed") {
						
					} else {
						if(callback){
							callback.call(this);
						}
					}
				}
			};
			xhrX.send("j_username=" + contentComment.userName
					+ "&j_password=" + contentComment.passWord + "");
		}else if(xhr.readyState == 4){
			
		};
	};
	xhr.onerror = function(){
		
	};
	xhr.ontimeout = function(){
		
	};
	xhr.send();
}