# Unable to fetch service from context

**URL:** <https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267>\
**Category:** Development\
**Created:** [February 18, 2021, 11:13am UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267 "2021-02-18T11:13:19Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![vishi](https://talk.openmrs.org/letter_avatar/vishi/32/6_22734026db12803ebb3e059622ac3534.png) [@vishi](https://talk.openmrs.org/u/vishi)\
**Post date:** [February 22, 2021, 3:32pm UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/21 "2021-02-22T15:32:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [February 22, 2021, 4:19pm UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/22 "2021-02-22T16:19:43Z")

</div>

Did you get a chance to look at its documentation here? [REST Web Services API For Clients - Documentation - OpenMRS Wiki](https://wiki.openmrs.org/display/docs/REST+Web+Services+API+For+Clients#RESTWebServicesAPIForClients-Authentication)

---

<div class="post-metadata">

**Author:** ![goylshubham](https://talk.openmrs.org/user_avatar/talk.openmrs.org/goylshubham/32/10878_2.png) [@goylshubham](https://talk.openmrs.org/u/goylshubham)\
**Post date:** [February 27, 2021, 7:13am UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/23 "2021-02-27T07:13:30Z")

</div>

Here’s the code while making request to the session. fetch(“[http://localhost:8080/openmrs/ws/rest/v1/session](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](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.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [February 27, 2021, 7:24pm UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/24 "2021-02-27T19:24:48Z")

</div>

What exact error message do you get in the browser?

---

<div class="post-metadata">

**Author:** ![goylshubham](https://talk.openmrs.org/user_avatar/talk.openmrs.org/goylshubham/32/10878_2.png) [@goylshubham](https://talk.openmrs.org/u/goylshubham)\
**Post date:** [March 1, 2021, 5:01am UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/25 "2021-03-01T05:01:30Z")

</div>

![image](https://talk.openmrs.org/uploads/default/original/2X/a/aae1aeafa7b2d0d948782d24afc3025365ea68c6.png)

I changed the url to [demo.openmrs.org](http://demo.openmrs.org).

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [March 1, 2021, 6:13pm UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/26 "2021-03-01T18:13:38Z")

</div>

You need to first log in.

---

<div class="post-metadata">

**Author:** ![goylshubham](https://talk.openmrs.org/user_avatar/talk.openmrs.org/goylshubham/32/10878_2.png) [@goylshubham](https://talk.openmrs.org/u/goylshubham)\
**Post date:** [March 12, 2021, 5:25am UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/27 "2021-03-12T05:25:35Z")

</div>

I did log in. Check this code [Unable to fetch service from context - #23 by goylshubham](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/23) 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.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [March 12, 2021, 8:20am UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/28 "2021-03-12T08:20:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ibacher](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ibacher/32/19751_2.png) [@ibacher](https://talk.openmrs.org/u/ibacher)\
**Post date:** [March 12, 2021, 2:49pm UTC](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267/29 "2021-03-12T14:49:46Z")

</div>

> [@goylshubham](#):
>
> Problem is that I am not able to use the JSESSIONID (returned by the session endpoint) in other requests.

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.,

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

```

[Previous page](https://talk.openmrs.org/t/unable-to-fetch-service-from-context/32267.md?page=1)
