Odata not working with custom authorization header

8 posts, 0 answers
  1. Gheorghe
    Gheorghe avatar
    1 posts
    Member since:
    Apr 2014

    Posted 22 Aug 2014 Link to this post

    Hi,

    I have a serious issue with odata requests and an authentication header I have to set - it just is not working! Here is the transport part of my datasource definition (done inside the grid configuration:

    transport: {
                      type: "odata",
                       read: {
                           url: "http://........",
                           beforeSend: function (xhr) {
                               var auth = 'Bearer ' + token;
                               xhr.setRequestHeader('Authorization', auth);
                           }
                       },
     
                   },

    If I remove the line saying "type = odata", then the header is correctly set (of course, the request is not delivering useful data then...). If I add the line, the "beforeSend" part is executed, but the header is not set in the request.

    Could this be a bug?

    Best regards,
    Gheorghe
  2. Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 25 Aug 2014 Link to this post

    Hi Gheorghe,

    As you may know when odata type is set, by default the request will be executed via JSONP. Therefore, due to the way JSONP works there is not way to set the authorization headers. However, if the service you are trying to access is in the same domain as the application or the CORS are enabled you should set the read request dataType option to json, which will then allow setting the authorization headers.

    transport: {
        type: "odata",
         read: {
             url: "http://........",
             dataType: "json",
             beforeSend: function (xhr) {
                 var auth = 'Bearer ' + token;
                 xhr.setRequestHeader('Authorization', auth);
             }
         },
     
     },


    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Ingo
    Ingo avatar
    1 posts
    Member since:
    Jan 2015

    Posted 17 Feb 2015 in reply to Rosen Link to this post

    Hi Rosen,

    I have exactly the same problem. My DataSource is initialized like this:
    kendo.data.DataSource({
            type: "odata-v4",
            transport: {
              read: {
                url: dataUrl,
                dataType: 'json',
                beforeSend: function (req) {
                  var auth = self.options.authenticationType + " " + self.options.authenticationToken;
                  req.setRequestHeader('Authorization', auth);
                }
              }
            }
          });

    The authorization header is not set however. Is this due to odata-v4?
  5. Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 18 Feb 2015 Link to this post

    Hi Ingo,

    The code you have pasted should assign the authorization headers as expected. Please provide a small runnable sample in which the issue can be observed locally.

    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. Bill
    Bill avatar
    1 posts
    Member since:
    Jun 2012

    Posted 12 Feb Link to this post

    I also have the same issue, my request headers are not being set. 
     
    type: "odata",
                transport: {
                    read: {
                        url: "https://....",
                        dataType: 'json',
                        beforeSend: function (req) {
                            req.setRequestHeader('Authorization', auth);
                            req.setRequestHeader('Access-Control-Allow-Origin', '*')
                        }
                    },

     

     

  7. Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 15 Feb Link to this post

    Hello Bill,

     

    Maybe you could provide  a small runnable sample which demonstrate the issue you are experiencing. Also you may test your service and AJAX request configuration by making the same request but using just jQuery.ajax. As you know, the DataSource remote transport uses jQuery.ajax to make AJAX requests, thus same configuration should work for both.

     

    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  8. Jason
    Jason avatar
    1 posts
    Member since:
    Aug 2014

    Posted 03 May Link to this post

    Is anyone going to answer this question.  I had to come up with a .NET hack to get this work.  If someone provides you with an example they would have to set up all the security piece as well.  That is unacceptable for this type of issue.  
  9. Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 04 May Link to this post

    Hello Jason,

    As mentioned in the initial message using the beforeSend is the way to modify the request header. I have mentioned that the DataSource is not directly involved in send the headers as the configuration is passed to the underlying jQuery ajax call behind the scenes. Thus, the same approach applicable with jQuery (and discussed here for example) can be applied to the DataSource too. This is why I have suggested that in order to locally troubleshoot the problem a jQuery ajax call can be used leaving aside the DataSource. Then the same configuration can be used with the DataSource. If it cannot be get to work with jQuery ajax, then the problem may be somewhere else, for example the configuration of the server. This is the steps to self troubleshoot the issue, otherwise a sample project (leaving the server-side of the things out as much as possible as it is not directly related to Kendo UI) which we can look  at locally will be required.

    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready