Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with AngularJS 1.4 and transformRequest and uploading to S3 #814

Closed
darkmavis1980 opened this issue Jun 12, 2015 · 12 comments
Closed

Comments

@darkmavis1980
Copy link

I have an issue today when I updated my project from Angular 1.3 to 1.4, and it's probably because now you can't modify the request header with transformRequest anymore (or at least according to this article).
Essentially before I was calling the upload function with these parameters:

        url: my-bucket-url,
        headers: {'Content-Type': file.type},
        method: 'POST',
        transformRequest: function (data, headersGetter) {
          // Headers change here
          var headers = headersGetter();
          delete headers.Authorization;
          return data;
        },
        fields: fieldsData,
        ...

As I had to remove the Authorization token from the header I had to transform my headers before sending the file to S3. But now I see that the Authorization is not getting removed from the headers.
Any idea how to fix it? Did anybody test ng-file-upload with Angular 1.4?

@danialfarid
Copy link
Owner

If you don't need the Authorization headers then don't add them to the request in first place.

@darkmavis1980
Copy link
Author

Unfortuantely it's not that easy, the Token is added to all the request as it's needed for authentication with the API, the only case where I don't need it is when I have to upload a file from the client directly to S3

@darkmavis1980
Copy link
Author

Found a solution, not beautiful, but it works.
Essentially I removed the transformRequest as is not working anymore in AngularJS 1.4, and I've added this code before I call the upload method

 // we store the auth token
 var storeAuth = $http.defaults.headers.common.Authorization;
 // we remove it temporarily from the headers
 delete $http.defaults.headers.common.Authorization;

Upload.upload({
...
});

// we restore the auth Token in the $http headers
$http.defaults.headers.common.Authorization = storeAuth;

Again, not a beautiful solution, but at least now I have the upload on S3 back working on my project

@nukulb
Copy link
Contributor

nukulb commented Jun 15, 2015

Better to replace transform request with just this

-                    transformRequest: function (data,headersGetter) {
-                        //Headers change here
-                        var headers = headersGetter();
-                        delete headers['Authorization']; //jshint ignore:line
-                        return data;
+                    headers: {
+                       'Authorization': undefined
                     },

@nukulb
Copy link
Contributor

nukulb commented Jun 15, 2015

@danialfarid I suggest closing this.

@nukulb nukulb mentioned this issue Jun 30, 2015
@galenp
Copy link

galenp commented Jul 1, 2015

This helped me but I think referencing the repo at nukulb/s3-angular-file-upload is a bit confusing as It feels like its outdated. Maybe update README?

@galenp
Copy link

galenp commented Jul 1, 2015

Specifically that example uses the transformRequest technique for removing the Authorization header.

@paulwithap
Copy link

Seems like more of an issue with your auth implementation than this library. I store my tokens elsewhere and set the auth header on the fly if the request url contains my API base url. Who knows what other services you may have issues with later? I doubt you want this transformRequest code in every single place you're hitting an endpoint other than your API.

@nukulb
Copy link
Contributor

nukulb commented Jul 9, 2015

@paulwithap Good point on checking the base URL before adding the auth header.
Most people I suspect are adding to auth header all the time. You would never run into this issue with your approach.

Thanks for the idea.

@darkmavis1980
Copy link
Author

@paulwithap All the calls to my API has to contain the token in the header, it would be silly to do a control every time in my opinion.
This was the only call in the entire application where I needed to remove the header simply because I wasn't sending the data to my API, but to Amazon S3, which refuses any connection if I send the token in the header.

@darkmavis1980
Copy link
Author

The point was not how my app should work, but the simple fact that the transformRequest doesn't work anymore with AngularJS 1.4 onwards, that's it

@Deklin
Copy link

Deklin commented Sep 22, 2015

We are having an issue with angular transform request and ADDING a header. This is a deprecation since angular 1.2.x which we upgraded from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants