Openvidu server API REST with fetch

779 views Asked by At

I start up KMS+OPENVIDU server with docker. Everything works nice with web component when i use JQ ajax:

For example (this works fine):

   $.ajax({
          type: "POST",
          url: OPENVIDU_SERVER_URL + "/api/sessions",
          data: JSON.stringify({ customSessionId: sessionId }),
          headers: {
            "Authorization": "Basic " + btoa("OPENVIDUAPP:" + OPENVIDU_SERVER_SECRET),
            "Content-Type": "application/json"
          },
          success: response => resolve(response.data.id),
          error: (error) => {
            if (error.status === 409) {
              resolve(sessionId);
            } else {
              console.warn('No connection to OpenVidu Server. This may be a certificate error at ' + OPENVIDU_SERVER_URL);
              if (window.confirm('No connection to OpenVidu Server. This may be a certificate error at \"' + OPENVIDU_SERVER_URL + '\"\n\nClick OK to navigate and accept it. ' +
                'If no certificate warning is shown, then check that your OpenVidu Server is up and running at "' + OPENVIDU_SERVER_URL + '"')) {
                location.assign(OPENVIDU_SERVER_URL + '/accept-certificate');
              }
            }
          }
        })

If i put fetch (native javascript) :

      var myPromise = fetch(this.ovServerUrl + '/api/sessions', {
        method: 'POST',
        // mode: 'cors',
        // credentials: 'same-origin',
        // redirect: 'follow',
        body: JSON.stringify(
          { customSessionId: sessionId }
        ),
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('OPENVIDUAPP:' + this.ovSecret)
        }
      })


      return myPromise.then(response => {
        console.log('return resolve !response', response)
        console.log('return resolve !response.data.id! ', response.data.id)
        return response.id
      })

I got response but without response.id .

Looks like :

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: ""
type: "cors"
url: "https://MY_DOMAIN:4443/api/sessions"

Any suggestion ?

0

There are 0 answers