Unable to fetch service from context

Do I need to pass as cookie or I can pass it in query string ?

Did you get a chance to look at its documentation here? REST Web Services API For Clients - Documentation - OpenMRS Wiki

Here’s the code while making request to the session. fetch(“http://localhost:8080/openmrs/ws/rest/v1/session”, { headers: { ‘Authorization’: Basic ${btoa('Admin:Admin123')} } }).then(response => { response.json().then(data => { sessionId = data.sessionId }) })

Now in order to use the same token/JSESSIONID return in the above request here’s the code we are using. fetch(“http://localhost:8080/openmrs/ws/rest/v1/user”, { headers: { ‘Cookie’: JSESSIONID=${sessionId} }, }).then(res => { console.log(res.json()); })

But this code won’t work. Because browser doesn’t allow you to set the cookie manually.

NOTE: The openmrs and the client application both are running on different domains.

What exact error message do you get in the browser?

I changed the url to demo.openmrs.org.

You need to first log in.

I did log in. Check this code Unable to fetch service from context - #23 by goylshubham First request to the session endpoint with authentication header is working fine. Problem is that I am not able to use the JSESSIONID (returned by the session endpoint) in other requests.

In other requests to the same exact server? How are you passing the session id?

JSESSIONID is intended to be used as a cookie and is only really useful from a cookie. Your best bet would be to ensure your front end is running on the same domain as OpenMRS and to use a simple proxying backend to set the JSESSIONID cookie.

If you really need a pure Javascript solution, you can try fetching the session id from the endpoint as you’ve done, then create a Javascript cookie and send your second request with credentials: 'include' option, e.g.,

document.cookie = "JSESSIONID=" + sessionId + "; path='/openmrs'";
fetch(..., { credentials: 'include' })