authorization header

1,162 views
Skip to first unread message

mple...@gmail.com

unread,
Jun 18, 2011, 11:29:24 AM6/18/11
to Amplify
Hello,

How do i add a custom authorization header in the amplify.request?

thx

Scott González

unread,
Jun 18, 2011, 12:49:49 PM6/18/11
to ampl...@googlegroups.com
You can use the beforeSend callback from jQuery.ajax:

amplify.request.define( "foo", "ajax", {
    url: "path/to/resource",
    beforeSend: function( xhr ) {
        xhr.setRequestHeader( "X-Amplify-Request", "bar" );
    }
});

See http://api.jquery.com/jQuery.ajax for a full set of options that work with amplify.request's ajax request type.

Alternatively, you can hook into the request.before.ajax message, though you'd need to filter based on the resource id:

amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSettings, xhr ) {
    if ( resource.resourceId === "foo" ) {
        xhr.setRequestHeader( "X-Amplify-Request", "bar" );
    }
});

mandar katre

unread,
Feb 25, 2013, 8:01:39 AM2/25/13
to ampl...@googlegroups.com
Hi Scott,

I used the beforeSend callback to set the request headers, but the decoder was not firing, so I had to return true to make it fire.

beforeSend: function (xhr) {
                     xhr.setRequestHeader("X-Authorize-Request", "OAuthToken");
                     // return true to make decoder fire
                     return true;
                 },

But in my decoder, xhr.getResponseHeader("X-Authorize-Request") returns null, so how do I modify the header response header in the decoder?
this.decoder = function (data, status, xhr, success, error) {            
            if (status === "success") { 
               var foo = xhr.getResponseHeader("X-Authorize-Request");
               var all = xhr.getAllResponseHeaders();               
                success(data, xhr);                
            } else if (status === "fail" || status === "error") {
                error(status, xhr);
                console.log(xhr.responseText);
            } else {
                error(status, xhr);
                console.log(xhr.responseText);
            }
        };

Scott González

unread,
Feb 25, 2013, 8:13:36 AM2/25/13
to ampl...@googlegroups.com
Response headers come from the server. You can't change what they are after the response has been received.


--
You received this message because you are subscribed to the Google Groups "Amplify" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amplifyjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

pratham tandon

unread,
Jun 16, 2014, 8:41:57 AM6/16/14
to ampl...@googlegroups.com
Hi,

I am trying to attach csrf token to all my outgoing ajax requests. If I do it by defining a "beforeSend" on each amplify request, everything works fine.
However, to avoid code repetition, I tried to listen to "request.before.ajax" and call the setRequestHeader there. For some reason, it won't attach to the header. Some digging into amplify and jquery code revealed that the "state" variable is set to 1 when it tries to add the value to request headers in jquery. I tried to find what this means but there is not much help online. This is my code if it can be of any help:


            amplify.subscribe("request.before.ajax", function (resource, settings, ajaxSettings, xhr) {
                // Skip for non-service ajax calls (E.g. while loading html templates).
                if (ajaxSettings.url.indexOf("/api") == -1) return;

                require(["config", "logger"], function (config, logger) {
                    // Append the CSRF token on all POSTs.
                    if (ajaxSettings.type == "POST") {
                        var
                            csrfToken = config.csrfToken,
                            csrfTokenValue = $('input[name="' + config.requestVerificationToken + '"]').val();
                        xhr.setRequestHeader(csrfToken, csrfTokenValue);
                    }
                    logger.url = ajaxSettings.url;
                    logger.data = ajaxSettings.data;
                });
            });

Can you pls suggest where I'm going wrong ?

Thanks!
Pratham 

Doug Neiner

unread,
Jun 17, 2014, 12:59:59 AM6/17/14
to ampl...@googlegroups.com
Hi Pratham,

I believe the RequireJS call you are making inside the subscription is the problem. The pub/sub for “request.before.ajax” is synchronous, so the call has already started to execute before you set the header. Try moving your `require` call around the amplify.subscribe call. That way the require will only happen once on boot, and the synchronous subscription should work as you expect.

Doug
--
You received this message because you are subscribed to the Google Groups "Amplify" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amplifyjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages