question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

multipart form data submit with restangular

See original GitHub issue

I have to submit a form which includes a user selected file to the server. I can get this working using $http directly as below,

    var fd = new FormData();
            fd.append("profile",angular.toJson(profile));
             fd.append("file", file);
            return $http.post("/server/api/profile/me/bio", fd, {
                withCredentials: true,
                headers: {'Content-Type': undefined },
                transformRequest: angular.identity
            });

I tried to do the same submit using restangular as below return userAPI.one(‘me’).customPOST(fd,“bio”,{}, {‘Content-type’:undefined});

However this doesn’t do the multipart submit correctly and attempts to send a empty payload request. I suspect this is because I need to specify the transformRequest flag to address this issue: https://github.com/angular/angular.js/issues/1587

Is there a way to do set a request transformer just on this request?

Thanks

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:27 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
andrzej-aacommented, Jun 1, 2016

I found a workaround for setting default headers. Instead of setting undefined, set a function returning undefined:

RestangularProvider.setDefaultHeaders({
  'Content-Type': 'application/json'
});


return API.all(UPLOAD_RESOURCE)
  .withHttpConfig({transformRequest: angular.identity})
  .customPOST(file, 'image', {}, {
    'Content-Type': () => {
      return undefined;
    }
  });
1reaction
ncourtialcommented, Jan 23, 2015

Hi, All seems to be OK when you don’t set global default headers. When adding the following default values in config step, form data are posted without requested headers : form-data and boundary but with ‘application/json’. RestangularProvider.setDefaultHeaders({ “Content-Type”: “application/json”, “X-Requested-With”: “XMLHttpRequest” });

When removing “Content-Type”: “application/json” from config , customPOST(formData,‘’,undefined,{‘Content-Type’: undefined}) does its stuff.

Any idea?

Many thanks in advance.

Cheers

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to upload a file in Restangularjs using multipart/formdata
This piece of code worked for me.... $scope.uploadFiles = function(file) { console.log(file); $scope.fileData = file; var fd = new FormData(); ...
Read more >
Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending...
Read more >
Ngx-restangular: RESTful API Angular Solution - 2muchcoffee
Ngx-restangular is a custom-made Angular 2+ solution that simplifies common GET, POST, DELETE, and UPDATE requests with a minimum of client code ...
Read more >
restangular-shlatchz - npm
Restangular is an AngularJS service that simplifies common GET, POST, DELETE, ... the request to use the Content-Type: multipart/form-data as the header.
Read more >
Angular 8 + Spring Boot: File upload example - BezKoder
Spring Boot Multipart File upload (to static folder) example ... FormData is a data structure that can be used to store key-value pairs....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found