1

I need to send custom headers to my wcf oData Service but with the following function the headers dont get modified.

entities.onReady(function () {

    entities.prepareRequest = function(r) {
        r[0].headers['APIKey'] = 'ABC';
    };

    entities.DataServiceClient.toArray(function (cli) {
        cli.forEach(function (c) {
            console.log(c.Name)
        });
    });
});

headers are not affected. any clue?

thanks!

Jmorvan
  • 1,020
  • 11
  • 31
  • just a quick check: is DataServiceClient an EntitySet? Could you show your request headers and context definition? This is working for me – Robesz Oct 09 '13 at 05:24
  • @Robesz Yes it is a an EntitySet, I have answered my post bellow. let me know if you have any other way! what version of JayData and dataJS are you using? – Jmorvan Oct 09 '13 at 12:33
  • I'M using JayData 1.3.2 and datajs 1.0.3 – Robesz Oct 09 '13 at 13:40

2 Answers2

0

EDIT

On second thought, it seems like there is still something broken in JayData for MERGE requests.

This is NOT CORS and has nothing to do with it!

see JayData oData request with custom headers - ROUND 2

the bellow "hack" works, but the above question should take this problem to a new level.

----------

Old answer

Nevermind I found a solution.

It seems like prepareRequest is broken in JayData 1.3.2 (ODataProvider).

As a hack, I added an extraHeaders object in the providerConfiguration (oDataProvider.js):

  this.providerConfiguration = $data.typeSystem.extend({
                   //Leave content unchanged and add the following:
                    extraHeaders: {}
                }, cfg);

then at line 865 modify requestData like this:

var requestData = [
                    {
                        requestUri: this.providerConfiguration.oDataServiceHost + sql.queryText,
                        method: sql.method,
                        data: sql.postData,
                        headers: _.extend({
                            MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
                        },this.providerConfiguration.extraHeaders)
                    },

NOTE: Iam using lodash for conveniance, any js extend should do the trick.

then you just create your client like this:

 var entities = new Entities.MyEntities({
            name: 'oData',
            oDataServiceHost: 'http://myhost.com/DataService.svc',
            maxDataServiceVersion: "2.0",
            //enableJSONP: true,
            extraHeaders: {apikey:'f05d1c1e-b1b9-5a2d-2f44-da811bd50bd5', Accept:'application/json;odata=verbose'}
        }
    );
Community
  • 1
  • 1
Jmorvan
  • 1,020
  • 11
  • 31
  • nice catch! I didn't know this bahavior of datajs – Robesz Oct 09 '13 at 13:42
  • @Robesz Might be new in datajs 1.1.1. I saw a few posts where it worked for people to just override prepareRequest. – Jmorvan Oct 11 '13 at 13:15
  • 1
    Actually this analysis is not correct. The normalizeHeaders function only normalizes the names of the headers listed in 'normalHeaders'. It ignores (doesn't modify or delete) all other headers. – Mark Lagendijk Dec 11 '13 at 15:31
  • thanks @Mark! You are right! `prepareRequest` is still broken though... But thats JayData's problem. dont' have the time to pin it down exactly. I edited my answer! – Jmorvan Dec 11 '13 at 21:24
0

It seems that the marked answer is incorrect. I was suffering from a similar issue, but got it working without changing datajs.

My issue was that I was doing a cross domain (CORS) request, but didn't explicitly allow the headers. After I added the correct CORS header to the webservice, it worked.

Mark Lagendijk
  • 6,247
  • 2
  • 36
  • 24
  • Working here for datajs 1.1.1 and JayData 1.3.4/5 as well. – RainerAtSpirit Jan 10 '14 at 08:41
  • @RainerAtSpirit Ok for GET requests! but what about MERGE requests? – Jmorvan Feb 10 '14 at 23:22
  • Good question. I'm talking to a .Net WebAPI OData v3 endpoint through a [hapijs](http://spumko.github.io/) based reverse proxy. OData v3 uses PATCH instead of MERGE, but I doubt that this makes a difference. If possible I'd try to avoid CORS in the first place and see if you can get it working that way. – RainerAtSpirit Feb 11 '14 at 08:16
  • @RainerAtSpirit I have to use V2, did you have look at this http://stackoverflow.com/questions/21689832/jaydata-odata-request-with-custom-headers-round-2 . there is definitelly something wrong and has nothing to do with CORS. – Jmorvan Feb 11 '14 at 13:27