Problem with headers

Feb 17, 2012 at 9:22 AM

Hi guys, I have the following problem:

1. Create headers for request e.g.

var headers = {Accept : "application/atomsvc+xml...n/json;q=0.5, */*;q=0.1" };

2.  use this header var for the next request e.g. read metadata or read data

After the request datajs has modified the headers variable and it now has the following content:

Accept "application/atomsvc+xml...n/json;q=0.5, */*;q=0.1"
Content-Type "application/json"
DataServiceVersion "2.0" 

3. do a POST request with the same header var

Accept "application/atomsvc+xml...n/json;q=0.5, */*;q=0.1"
Content-Type "application/json"
DataServiceVersion "1.0" 

4. Do a Read request with the same header var...This request fails because datajs has modified the header var.

--> Read request fails because the server expects DataServiceVersion 2.0.

 

==> So is this a datajs bug where datajs doesn't make a copy of the header parameters and modifies the passed header reference variable or do I have to create a new header every time I do a read/write request?

THX


 

Feb 18, 2012 at 12:43 AM

Hi Joe,

   Hope you are doing great.  First things first :).  The library will modify the headers object by only adding those headers that were missing from it and required for the request.  So for example, if you pass a headers object that doesn't have a DataServiceVersion value, then the library will add one with a sensible default, based on the request payload.  On the other hand, if the headers object already has a DataServiceVersion value the library will pass it along and will not modify it.  I tried to repro the behavior you mention between step 2 and 3:  that the library modified the header DataServiceVersion from "2.0" to "1.0" when doing a POST request. However, this behavior doesn't repro for me.  It would be great If you could share with us a sample code of how are you invoking these requests so I can investigate further.

Regards,

Alex Trigo.

 

Feb 20, 2012 at 1:22 PM
Edited Feb 20, 2012 at 2:07 PM

Hi Alex,

hope you are doing great also :)

I retried and now I think I got the problem. In the first place my header var didn't include a DataServiceVersionvalue.

If I always reuse my header variable and do a PUT request the following request headers are sent:

PUT http://services.odata.org/(S(wj5boqtjql2dfjjcqmfy3i1y))/OData/OData.svc/Products(2) HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: application/atomsvc+xml;q=0.8, application/json;q=0.5, */*;q=0.1
Accept-Language: en
Accept-Encoding: gzip, deflate
Connection: keep-alive
DataServiceVersion: 1.0
Content-Type: application/json; charset=UTF-8
Content-Length: 669

Now if I reuse my header var (it has now DataServiceVersion: 1.0) and do the following request, it fails:

 GET http://services.odata.org/(S(wj5boqtjql2dfjjcqmfy3i1y))/OData/OData.svc/Products?$skip=0&$top=1&$inlinecount=allpages HTTP/1.1

Host: localhost:8080
 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: application/json
Accept-Language: en
Accept-Encoding: gzip, deflate
Connection: keep-alive
DataServiceVersion: 1.0
Content-Type: application/json

Because in 1.0 the skip/top etc is not supported.

So what would be the best solution?

--> always use a new header variable?

--> set the version to 2.0...then datajs will set the version to 1.0 for PUT but won't modify my header variable...right? so in the next getRequest the version is again 2.0...

 OK here is the sample code...yeah it's a bit messy ;)

Please debug and inspect the requests...you will see that the PUT and the following GET request will have DataServiceVersion = 1.0

 

var sMetadataURL = "http://services.odata.org/(S(wj5boqtjql2dfjjcqmfy3i1y))/OData/OData.svc/$metadata";
var sUrl = "http://services.odata.org/(S(wj5boqtjql2dfjjcqmfy3i1y))/OData/OData.svc/Products";

var oHeaders = {};
request = {
headers : oHeaders, // object that contains HTTP headers as name value pairs
requestUri : sUrl, // OData endpoint URI
};

OData.read(sMetadataURL, function (metadata) {
OData.read(request, function (data, response) {
debugger;

var entry = data.results[0];
// modify data
entry.Name = "Doe";
var updateUrl = entry.__metadata.uri;

request = {
headers : oHeaders, // object that contains HTTP headers as name value pairs
requestUri : updateUrl, // OData endpoint URI
method : "PUT", // HTTP method (GET, POST, PUT, DELETE)
data : entry // Payload of the request (in intermediate format)
};

OData.request(request, function (data, response) {
debugger;
var readURL = "http://services.odata.org/(S(wj5boqtjql2dfjjcqmfy3i1y))/OData/OData.svc/Products";
request = {
headers : oHeaders, // object that contains HTTP headers as name value pairs
requestUri : readURL, // OData endpoint URI
method : "GET", // HTTP method (GET, POST, PUT, DELETE)
};
OData.read(request, function (data, response) {
debugger;
}, function(err){
alert(err)
}, undefined, undefined, metadata);

}, function(err){
alert(err);
}, undefined, undefined, metadata);
}, function(err){
alert(err);
}, undefined, undefined, metadata);
}, function (error) {
alert(error);
}, OData.metadataHandler);

 

Feb 22, 2012 at 12:54 AM
Edited Feb 22, 2012 at 12:57 AM

Hi Joe,

   I'm doing good too, thanks :). 

So what would be the best solution?

--> always use a new header variable?

--> set the version to 2.0...then datajs will set the version to 1.0 for PUT but won't modify my header variable...right? so in the next getRequest the version is again 2.0...

   I'd say that the best solution in this case is to set the DataServiceVersion value in your headers collection to "2.0" from the start... you already know that you are targeting an OData V2 endpoint anyway :). DataJS will pickup this value in all the requests and will not modify it to 1.0 in the PUT (because you are overriding the default ) so the 2.0 will stay put on all the requests.

Regards,

Alex Trigo.

  

Feb 22, 2012 at 11:24 AM

Ok thanks Alex. I'll do so.

Feb 12, 2015 at 1:18 PM
What about custom headers?

If have the following in code, but the request header does not include the information:
    var _headers = {};
    _headers["subscription-key"] = "XXXXXXXXXXXXXXXXXX";
    var request = {
        headers: _headers, // object that contains HTTP headers as name value pairs 
        requestUri: _url // OData endpoint URI 
    };
Do I need to do something else?

Best regards,
Peter