Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

How can I set the content-type as application/json for DELETE method in $resource #13290

Closed
mdibay1367 opened this issue Nov 10, 2015 · 6 comments

Comments

@mdibay1367
Copy link

I'm trying to set the content-type as application/json for $resrouce delete action. The only reason that I need to enforce the content-type to application/json is that IE10, and IE11 detects the content-type for DELETE request as plain/text, but all other browsers and older versions of IE detects it as application/json. I have a limitation from back end, which always check for content-type as Json.

Here is how i'm trying to enforce it:

remove: {
method: 'DELETE',
headers: { 'Content-Type': 'application/json; charset=utf-8'},
transformRequest: function(data, headerGetters) {
headerGetters()['Content-Type'] = 'application/json';
return angular.toJson(data);
},
update: {
method: 'PUT'
}

But this still doesn't work and couldn't set the content-type as json.

@gkalpak
Copy link
Member

gkalpak commented Nov 11, 2015

This is not Angular-specific, i.e. you can replicate it with "raw" XMLHttpRequest.
Closing as it is not actionable on the Angular side.

@gkalpak gkalpak closed this as completed Nov 11, 2015
@aghreed
Copy link

aghreed commented Nov 19, 2015

@gkalpak, I'm a little confused how this is not angular specific when this is not an issue with angular v1.3.15.

I'm in a similar situation as @mdibay1367 where I don't have control over the API I'm hitting, and it is expecting "Content-Type" : "application/json". Any other browser besides IE10/IE11 is not sending a "Content-Type" header at all, but IE10/IE11 insists on attaching a "Content-Type": "text/plain".

Again, this issue does not exist with v1.3.15... are we sure this is an issue that should be closed?

@gkalpak
Copy link
Member

gkalpak commented Nov 20, 2015

@aghreed , could you, please, provide a live demo (e.g. using CodePen, Plnkr etc) of this working on 1.3.15 ?
I'd be happy to re-open this and investigate further if it turns out to be Angular-specific.

@kevin-king
Copy link

Edit: looks like this issue is being tracked here: #2149

Using Angular v1.4.3. IE11 bug. Not occurring on IE9 or any other browser. Did not check IE10.

For what it's worth, I'm having this exact same problem with $http delete. There is a known bug where certain IE versions will treat the "delete" in $http.delete as a JavaScript keyword instead of a method. The workaround is to use $http['delete'].

Both of these methods, however, have the exact same issue that mdibay described, as well as $http({method: 'DELETE', url: ...}).

I tried explicitly adding 'Content-Type': 'application/json' both on the $httpProvider default headers and in the above object-style $http({headers: {'Content-Type': 'application/json'}}). Also tried lowercased 'content-type' and all permutations of syntax, as suggested by a Stack Overflow post.

@kevin-king
Copy link

Was able to fix my related problem by setting the message body to null, which removes the Content-Type header in IE11

$http({
method: 'DELETE',
data: null,
url: ...
})

@jakubmank
Copy link

For me it was enough to prepare following request
$http.delete(url, { data: {name: 'name to be deleted}, headers: {'Content-type': 'application/json'} })

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

No branches or pull requests

5 participants