Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap FileTransfer with HTTP basic authentication

I'm attempting to upload a file from PhoneGap to a server using the FileTransfer method. I need HTTP basic auth to be enabled for this upload.

Here's the relevant code:

    var options = new FileUploadOptions({
        fileKey: "file",
        params: {
            id: my_id,
            headers: { 'Authorization': _make_authstr() }
        }
    });
    var ft = new FileTransfer();
    ft.upload(image, 'http://locahost:8000/api/upload', success, error, options);

Looking over the PhoneGap source code it appears that I can specify the authorization header by including "headers" in the "params" list as I've done above:

      JSONObject headers = params.getJSONObject("headers");
      for (Iterator iter = headers.keys(); iter.hasNext();)
      {
        String headerKey = iter.next().toString();
        conn.setRequestProperty(headerKey, headers.getString(headerKey));
      }

However, this doesn't seem to actually add the header.

So: is there a way to do HTTP basic auth with PhoneGap's FileTransfer, for both iPhone and Android?

like image 507
Parand Avatar asked Apr 25 '12 19:04

Parand


1 Answers

You can add custom headers by adding them to the options rather than the params like so:

authHeaderValue = function(username, password) {
    var tok = username + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
};

options.headers = {'Authorization': authHeaderValue('Bob', '1234') };
like image 189
Ryan Avatar answered Oct 19 '22 02:10

Ryan