Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DataSource transformResponse() and HTTP response headers

    Hi,

    I have a REST backend used for my 'fetch' operation. The backend provides HTTP response headers (RANGE)
    that provide data paging information (startRow, endRow, totalRows).

    Within a DataSource.transformResponse() method, how can I access the HTTP response header to extract
    those informations ? The HTTP response headers don't seem to be available from within that DS method.

    Thanks,

    #2
    Sorry, this looks like it wasn't documented, but the headers are available as dsResponse.httpHeaders, same format as dsRequest.httpHeaders. Note that headers are only available with the default xmlHttpRequest protocol and browsers differ in which headers they allow you to see.

    Comment


      #3
      That's strange. I have my data source on which I've set dataTransport to 'xmlHttpRequest' (which I think is default anyways),
      but inside transformResponse(), dsResponse.httpHeaders is undefined ...

      Any idea ?

      Code:
      var BaseDataSource = isc.defineClass('BaseDataSource', 'DataSource');
      
      //-----------------------------
      // Instance class members
      //-----------------------------
      BaseDataSource.addProperties
      ({
      	dataTransport : 'xmlHttpRequest',
      
      	requestProperties :
      	{
      		httpHeaders : { 'Accept' : 'application/json' }
      	},
      
      	operationBindings :
      	[
      		{ operationType : 'fetch', dataProtocol : 'getParams', dataFormat : 'json', requestProperties : { httpMethod : 'GET' } },
      		{ operationType : 'remove', dataProtocol : 'getParams', dataFormat : 'custom', requestProperties : { httpMethod : 'DELETE' } },
      		{ operationType : 'add', dataProtocol : 'postMessage', dataFormat : 'json', requestProperties : { httpMethod : 'PUT' } },
      		{ operationType : 'update', dataProtocol : 'postMessage', dataFormat : 'json', requestProperties : { httpMethod : 'PUT' } },
      	],
      
      	//--------------------------------------------------------------------------------------------------------------------------------
      	transformRequest : function(dsRequest)
      	{
      		// always supply basic authentication credentials on all requests
      
      		dsRequest.httpHeaders['Authorization'] = 'Basic ' + Cookie.get(MeiApplication.authTokenCookieName);
      
      		// for insert/update/delete requests, alter URL to include entity identifier/key on which operation needs to be performed
      
      		if (dsRequest.operationType == 'remove' || dsRequest.operationType == 'add' || dsRequest.operationType == 'update')
      		{
      			var keyFld = this.getPrimaryKeyField();
      			if (keyFld)
      				dsRequest.dataURL = this.dataURL + '/' + dsRequest.data[keyFld.name];
      
      			// for delete requests, don't include the entity data as part of the request payload (saves bandwidth)
      
      			if (dsRequest.operationType == 'remove')
      				dsRequest.data = null;
      		}
      
          	return dsRequest.data;
      	},
      
      	//--------------------------------------------------------------------------------------------------------------------------------
      	transformResponse : function(dsResponse, dsRequest, data)
      	{
      		// succesfull fetch operation may return "HTTP 206 - Partial Content" and requires appropriate handling
      
      		if (dsRequest.operationType == 'fetch') //  && dsResponse.hhtpResponseCode == 206)
      		{
      			var headers = dsResponse.httpHeaders;
      		}
      
      		// succesfull remove/delete operation returns "HTTP 204 - No Content" and requires appropriate handling
      
      		else if (dsRequest.operationType == 'remove' && dsResponse.httpResponseCode == 204)
      		{
      			var keyFld = this.getPrimaryKeyField();
      			dsResponse.data = {};
      			dsResponse.data[keyFld.name] = dsRequest.originalData[keyFld.name];
      		}
      
          	return dsResponse;
      	}
      });
      Thanks,

      Comment


        #4
        Hi,

        Can anyone provide a solution? I desperately need this and don't seem to be able to find a place where I can get a hold
        of the HTTP headers within the DataSource transformResponse() method.

        Thanks!

        Comment


          #5
          For now, this is the only way I figured out how to get a hold of the HTTP response
          headers within the DataSource.transformResponse() method call ...

          Code:
           var transaction = RPCManager.getTransaction(dsResponse.transactionNum);
           var responseHeaders = transaction.responses[0].httpHeaders;
          I'm not even sure about the transaction.responses array accessor ... Why would there be more than
          one response for a given transaction number ? This relies on undocumented code and I'm sure
          there has to be a more straight forward and elegant way to do this ... Right ?

          I need this ... Thanks for your response,

          Comment

          Working...
          X