none
Accessing a Queue from JavaScript with jQuery

    Question

  • I want to take a peek for a message on a queue using the REST API. I have managed to create the correct signature to set on my Authorization header using Google's Crypto javascript library, but I keep on getting "HTTP Error 400. The request is badly formed."

    Here's the code I have, I left the comments in there because they might help.

    $(function () {
        var testButton = $("#testbutton");
        testButton.click(function () {
            var req = "";
            var d = new Date();
            var gmtDateString = d.toGMTString().replace("UTC", "GMT"); // have no clue why it's outputting UTC at the end of the string.
    
            var apiVersion = "2009-09-19";
            var key = "my shared key";
            var stringToSign =
                "GET\n" + /*HTTP Verb*/
                "\n" +    /*Content-Encoding*/
                "\n" +    /*Content-Language*/
                "\n" +    /*Content-Length*/
                "\n" +    /*Content-MD5*/
                "\n" +    /*Content-Type*/
                "\n" +    /*Date*/
                "\n" +    /*If-Modified-Since */
                "\n" +    /*If-Match*/
                "\n" +    /*If-None-Match*/
                "\n" +    /*If-Unmodified-Since*/
                "\n" +    /*Range*/
                /* CanonicalizedHeaders */
                "x-ms-date:" + gmtDateString + "\n" +
                "x-ms-version:" + apiVersion + "\n" +
                /* CanonicalizedResource */
                "/infinitywebstats/customerconnect/messages\n" +
                "peekonly:true\n";            
    
            var hmac = Crypto.HMAC(Crypto.SHA256, stringToSign, key, { asString : true });
    
            $.support.cors = true; // for cross domain.
    
            $.ajax({
                type: "GET",            
                url: "http://infinitywebstats.queue.core.windows.net/customerconnect/messages?peekonly=true",
                headers: {
                    "Authorization": "SharedKey infinitywebstats:" + hmac,                             
                    "x-ms-date": gmtDateString, "x-ms-version": apiVersion,                                
                },
                success: function (data, textStatus, jqXhr) {
                    alert(data);
                },
                error: function (jqXhr, textStatus, errorThrown) {
                    alert(jqXhr.responseText);
                }
            });
        });
    });


    What I am doing wrong here?

     

    Thanks.

    Friday, September 30, 2011 1:54 PM

Answers

  • I found out the solution, it was my signature string:

    var byteKey = Crypto.util.base64ToBytes(key);
    var hmacStr = Crypto.util.bytesToBase64
                (Crypto.HMAC(Crypto.SHA256, stringToSign, byteKey, { asBytes : true }));
    

    And now the hmacStr would be used in my Authorization header.

    • Marked as answer by sljc Friday, September 30, 2011 6:57 PM
    Friday, September 30, 2011 6:57 PM