You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: angular/angular.js#1587
Is there a way to do set a request transformer just on this request?
I updated to the latest version and changed to use withHttpConfig as you mentioned, but it still didn't quite work. Now the request payload is not empty but has the string "[object Object]". Note that the object I am passing in ("fd") is a FormData object.
Everything seems to be the same except the "data" attribute shows up in the debugger as "Object" instead of "FormData" when I used $http directly and put a breakpoint in angular.js. Since it's no longer a FormData object I think $http skips all the multipart handling stuff.
I wasn't able to figure out where the passed in FormData object was being changed in the restangular codebase. When a formdata object is passed in think it should just preserve it.
This thread just saved my bacon, but I wasted some time trying to figure out how to set the headers correctly. Here's a solution that worked for me. It's Coffeescript. Sorry. Not my choice.
postIt = () ->
formData = new FormData()
formData.append('file', file) # file is an ArrayBuffer read with fileReader.readAsArrayBuffer(file)
formData.append('name', file.name)
apiAuth.one('api/client', client.id).one('note', note.id).withHttpConfig({transformRequest: angular.identity}).customPOST(formData, 'file', undefined, {'Content-Type': undefined}).then (res)->
deferred.resolve(res)
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.
I'm trying to implement this but can't figure it out with my use case since it seems that Restangular.service('entities') doesn't have a valid .withHttpConfig() method.
How can achieve that functionality using the service approach?
It seems that Restangular.service('entities') only exports getList(), get(), post() methods :(
Nevertheless, in my Entities service I changed Restangular.service('entities') for Restangular.all('entities') and the file upload worked like a charm.
Thanks for comments and code snippets. It helps quite a lot!
I have a working implementation but I'm halfly satified because it uses Base64-encoded data upload which seems less efficient than Multipart Upload, and would not work with heavy file (as far as I understood).
@deltaepsilon I tried your code but it only sends the file itself, whereas I would like to update my user during the same call. It should be possible as that's what a regular form does.
I'm using jsPDF to create a PDF then i try to submit it with return Restangular.all('users').one('pdf', id).withHttpConfig({transformRequest: angular.identity}).customPOST(fd, 'submit', undefined, {'Content-Type': undefined});
But the header is application/json and i need a multipart/*
If i force the Content-Type to multipart/form-data i have a boundary problem. Is there a workaround ?
if i set ["Content-Type"] = undefined, It calls webservice , but i i want to send text data in other language (utf-8) it won't receive as expected in Rest api
Activity
mgonto commentedon Nov 21, 2013
Hey,
You can do that in the latest version of Restangular
That should work for you :).
Also, all other $httpConfigurations can be set in that
withHttpConfigcall as a parameter.Please tell me if it worked and reopen if it didn't
zoheb commentedon Nov 21, 2013
Thanks for the reply.
I updated to the latest version and changed to use withHttpConfig as you mentioned, but it still didn't quite work. Now the request payload is not empty but has the string "[object Object]". Note that the object I am passing in ("fd") is a FormData object.
mgonto commentedon Nov 21, 2013
Did you set there the withCredentials and contentType as well?
It should be the same with those.
Can you debug https://github.com/mgonto/restangular/blob/master/src/restangular.js#L457 and check what's being sent in to $http? You can compare that to see what's going on.
zoheb commentedon Nov 21, 2013
Everything seems to be the same except the "data" attribute shows up in the debugger as "Object" instead of "FormData" when I used $http directly and put a breakpoint in angular.js. Since it's no longer a FormData object I think $http skips all the multipart handling stuff.
I wasn't able to figure out where the passed in FormData object was being changed in the restangular codebase. When a formdata object is passed in think it should just preserve it.
zoheb commentedon Nov 21, 2013
Typo - I meant is shows up as "Object" when using restangular, but shows up correctly as FormData object when using $http
mgonto commentedon Nov 21, 2013
I know what it's. The problem is that I'm stripping Restangular stuff. As a hack go to elemFunction and remove StripRestangular part.
If that works Ill get it fixed :)
—
Mobile mail. Excuse brevity and typos.
On Thu, Nov 21, 2013 at 8:28 PM, zohebsait notifications@github.com
wrote:
zoheb commentedon Nov 21, 2013
Yes! I temporarily commented out lines 918-921 shown below and it's works! I will work around till you push a fix. Thanks for your help.
mgonto commentedon Nov 21, 2013
Thank you.
Ill leave this open meanwhile
—
Mobile mail. Excuse brevity and typos.
On Thu, Nov 21, 2013 at 8:44 PM, zohebsait notifications@github.com
wrote:
alonisser commentedon Dec 4, 2013
was this fixed?
mgonto commentedon Dec 4, 2013
Not yet, please comment those lines temporarily. I haven’t had much time lately to work on this bugs, but I’ll try to work on them ASAP.
Martin Gontovnikas
Software Engineer
Buenos Aires, Argentina
Twitter: @mgonto (https://twitter.com/mgonto)
Linkedin: http://www.linkedin.com/in/mgonto
Github: https://github.com/mgonto
On Wednesday, December 4, 2013 at 9:58 AM, Alonisser wrote:
mgonto commentedon Dec 9, 2013
Pushing fix :)
alonisser commentedon Dec 9, 2013
great!
Twitter:@alonisser https://twitter.com/alonisser
LinkedIn Profile http://www.linkedin.com/in/alonisser
Facebook https://www.facebook.com/alonisser
_Tech blog:_4p-tech.co.il/blog
_Personal Blog:_degeladom.wordpress.com
Tel:972-54-6734469
On Mon, Dec 9, 2013 at 10:49 PM, Martin Gontovnikas <
notifications@github.com> wrote:
deltaepsilon commentedon Jan 24, 2014
This thread just saved my bacon, but I wasted some time trying to figure out how to set the headers correctly. Here's a solution that worked for me. It's Coffeescript. Sorry. Not my choice.
gen4sp commentedon Jan 18, 2015
It still here, guys. I have same problem with 1.4
What i can do with it?
ncourtial commentedon 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
Tallyb commentedon Jan 24, 2015
+1
diosney commentedon Feb 4, 2015
Hi!
I'm trying to implement this but can't figure it out with my use case since it seems that
Restangular.service('entities')doesn't have a valid.withHttpConfig()method.How can achieve that functionality using the service approach?
Thanks in advance.
diosney commentedon Feb 6, 2015
It seems that
Restangular.service('entities')only exportsgetList(), get(), post()methods :(Nevertheless, in my Entities service I changed
Restangular.service('entities')forRestangular.all('entities')and the file upload worked like a charm.steinerj commentedon Mar 2, 2015
I'm having the exact same issue with using httpConfigs for objects created with .service
I have provided a PR, because I think this is a bit of a inconsistency. See #1068
augnustin commentedon Mar 3, 2015
Thanks for comments and code snippets. It helps quite a lot!
I have a working implementation but I'm halfly satified because it uses Base64-encoded data upload which seems less efficient than Multipart Upload, and would not work with heavy file (as far as I understood).
@deltaepsilon I tried your code but it only sends the file itself, whereas I would like to update my user during the same call. It should be possible as that's what a regular form does.
Here's the code:
View:
Controller:
Simply calling
$scope.user.put()sends a Base64-encoded image as a user.avatar attributes, which works Ok in my backend.But how can I have a
customPUTthat sends over the user attributes + the formData - user.avatar (otherwise it is sent twice)?I'll report final working solution. Thanks a lot!
ArthurianX commentedon May 29, 2015
Any progress on this? I'm having the same problem. :(
Waxo commentedon Jun 19, 2015
I'm using jsPDF to create a PDF then i try to submit it with
return Restangular.all('users').one('pdf', id).withHttpConfig({transformRequest: angular.identity}).customPOST(fd, 'submit', undefined, {'Content-Type': undefined});But the header is application/json and i need a multipart/*
If i force the Content-Type to multipart/form-data i have a boundary problem. Is there a workaround ?
rhodul commentedon Oct 16, 2015
@ncourtial, the default headers problem is fixed like so:
postFile: function (dataUri, path, keyName, fileName) {
var authenticatedHeaders = angular.extend({},
DEFAULT_HTTP_HEADERS,
{"x-authtoken": localStorage.authtoken});
authenticatedHeaders["Content-Type"] = undefined;
var formData = new FormData();
var blob = dataURItoBlob(dataUri);
formData.append(keyName, blob, fileName);
// kill default headers
var restangularRoute = Restangular.withConfig(function (RestangularConfigurer) {
RestangularConfigurer.setDefaultHeaders(authenticatedHeaders);
});
return restangularRoute.one(path).withHttpConfig({transformRequest: angular.identity})
.customPOST(formData, '', undefined, authenticatedHeaders);
}
suvarnajayanth commentedon Mar 18, 2016
if i set ["Content-Type"] = undefined, It calls webservice , but i i want to send text data in other language (utf-8) it won't receive as expected in Rest api
andrzej-aa commentedon Jun 1, 2016
I found a workaround for setting default headers. Instead of setting undefined, set a function returning undefined:
abbood commentedon Mar 2, 2018
I can't seem to have it work.. this is what I'm using:
I'm using restangular 1.51 with angular 1.6. My html looks like this
and the angular code:
laravel code:
thing is the $file here is appearing as an empty array in the laravel dump. The documentation is pretty bad on this. Ideas?
abbood commentedon Mar 3, 2018
so basically i had to brute force the content-type to
application/x-www-form-urlencoded..btw i'm using restangular 1.51.. more details here https://stackoverflow.com/questions/49077674/how-to-upload-files-with-angular-using-cors/49081727#49081727