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

Can't set headers for DELETE method #509

Closed
genomics-geek opened this issue Oct 29, 2016 · 9 comments
Closed

Can't set headers for DELETE method #509

genomics-geek opened this issue Oct 29, 2016 · 9 comments

Comments

@genomics-geek
Copy link

I create an instance of axios:
const api = axios.create({ baseURL: process.env.BASE_URL, timeout: 1000, });

I am able to set the header for POST:
api.defaults.headers.post['X-CSRFToken'] = cookie.csrftoken;

However, if I try to set the header for DELETE:
api.defaults.headers.delete['X-CSRFToken'] = cookie.csrftoken;

I get the following error:
Cannot set property 'X-CSRFToken' of undefined

Is this expected behavior?

@nickuraltsev
Copy link
Member

Fixed via d963368

@tangshuang
Copy link

tangshuang commented Aug 1, 2017

Hi @nickuraltsev , I am facing the same problem now. My case:

axios.defaults.headers.common['X-Request-Id'] = window.REQUEST_ID
axios.delete(url)

X-Request-Id is not sent when using delete method.
Could you have a look?

@ahlusar1989
Copy link

@tangshuang Do you mind demonstrating the full steps to reproduce this.

@tangshuang
Copy link

tangshuang commented Aug 14, 2017

Well, I found the reason.
DELETE method request should not be sent with non-emtpy body. When I sent a delete request I set the request body to be null, which contains contect in fact. After I remove the data option in config, it works correctly.
Thanks @ahlusar1989 .

@dnieh
Copy link

dnieh commented Sep 11, 2017

You could in theory use the axios.delete method correctly and pass in data with the following:

const globalAxiosConfig = {
  headers: {
    'X-CSRFToken': 'add csrf token here',
  },
}

async function axiosDelete(url, data = {}) {
  const axiosConfig = {
    ...globalAxiosConfig,
    ...data,
  }
  return await axios.delete(url, axiosConfig)
}

However, even with data present, Axios seems to ignore it in the request which makes sense since passing back data on a HTTP DELETE request was not defined in the HTTP 1.1 spec: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.7

Bodies on DELETE requests have no defined semantics. Note that sending a body on a DELETE request might cause some existing implementations to reject the request

@moeinrahimi
Copy link

in my case doing delete with axios in below method doesn't send header.

       let {data} = await axios.delete(baseUrl+`/likepost`,
       {
         postId:postId
       },{headers:{Cookie:`sid=${cookie}`}})

but this way it's working correctly

  let {data} = await axios({
        url:baseUrl+`/likepost`,
        method:'delete',
        data : {postId},
        headers:{Cookie:`sid=${cookie}`}
      })

@tangshuang
Copy link

tangshuang commented Mar 9, 2018

@moeinrahimi Are you sure? You mean you can post data with DELETE method?
Have you ever tried adding a new header option, like X-Request-Id.

jimthedev pushed a commit to commitizen/cz-cli that referenced this issue May 24, 2018
This Pull Request updates dependency [axios](https://github.com/axios/axios) from `v0.15.2` to `v0.18.0`



<details>
<summary>Release Notes</summary>

### [`v0.18.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0180-Feb-19-2018)
[Compare Source](axios/axios@v0.17.1...v0.18.0)
- Adding support for UNIX Sockets when running with Node.js ([#&#8203;1070](`axios/axios#1070))
- Fixing typings ([#&#8203;1177](`axios/axios#1177)):
    - AxiosRequestConfig.proxy: allows type false
    - AxiosProxyConfig: added auth field
- Adding function signature in AxiosInstance interface so AxiosInstance can be invoked ([#&#8203;1192](`axios/axios#1192), [#&#8203;1254](`axios/axios#1254))
- Allowing maxContentLength to pass through to redirected calls as maxBodyLength in follow-redirects config ([#&#8203;1287](`axios/axios#1287))
- Fixing configuration when using an instance - method can now be set ([#&#8203;1342](`axios/axios#1342))

---

### [`v0.17.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0171-Nov-11-2017)
[Compare Source](axios/axios@v0.17.0...v0.17.1)
- Fixing issue with web workers ([#&#8203;1160](`axios/axios#1160))
- Allowing overriding transport ([#&#8203;1080](`axios/axios#1080))
- Updating TypeScript typings ([#&#8203;1165](`axios/axios#1165), [#&#8203;1125](`axios/axios#1125), [#&#8203;1131](`axios/axios#1131))

---

### [`v0.17.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0170-Oct-21-2017)
[Compare Source](axios/axios@v0.16.2...v0.17.0)
- **BREAKING** Fixing issue with `baseURL` and interceptors ([#&#8203;950](`axios/axios#950))
- **BREAKING** Improving handing of duplicate headers ([#&#8203;874](`axios/axios#874))
- Adding support for disabling proxies ([#&#8203;691](`axios/axios#691))
- Updating TypeScript typings with generic type parameters ([#&#8203;1061](`axios/axios#1061))

---

### [`v0.16.2`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0162-Jun-3-2017)
[Compare Source](axios/axios@v0.16.1...v0.16.2)
- Fixing issue with including `buffer` in bundle ([#&#8203;887](`axios/axios#887))
- Including underlying request in errors ([#&#8203;830](`axios/axios#830))
- Convert `method` to lowercase ([#&#8203;930](`axios/axios#930))

---

### [`v0.16.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0161-Apr-8-2017)
[Compare Source](axios/axios@v0.16.0...v0.16.1)
- Improving HTTP adapter to return last request in case of redirects ([#&#8203;828](`axios/axios#828))
- Updating `follow-redirects` dependency ([#&#8203;829](`axios/axios#829))
- Adding support for passing `Buffer` in node ([#&#8203;773](`axios/axios#773))

---

### [`v0.16.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0160-Mar-31-2017)
[Compare Source](axios/axios@v0.15.3...v0.16.0)
- **BREAKING** Removing `Promise` from axios typings in favor of built-in type declarations ([#&#8203;480](`axios/axios#480))
- Adding `options` shortcut method ([#&#8203;461](`axios/axios#461))
- Fixing issue with using `responseType: 'json'` in browsers incompatible with XHR Level 2 ([#&#8203;654](`axios/axios#654))
- Improving React Native detection ([#&#8203;731](`axios/axios#731))
- Fixing `combineURLs` to support empty `relativeURL` ([#&#8203;581](`axios/axios#581))
- Removing `PROTECTION_PREFIX` support ([#&#8203;561](`axios/axios#561))

---

### [`v0.15.3`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0153-Nov-27-2016)
[Compare Source](axios/axios@v0.15.2...v0.15.3)
- Fixing issue with custom instances and global defaults ([#&#8203;443](`axios/axios#443))
- Renaming `axios.d.ts` to `index.d.ts` ([#&#8203;519](`axios/axios#519))
- Adding `get`, `head`, and `delete` to `defaults.headers` ([#&#8203;509](`axios/axios#509))
- Fixing issue with `btoa` and IE ([#&#8203;507](`axios/axios#507))
- Adding support for proxy authentication ([#&#8203;483](`axios/axios#483))
- Improving HTTP adapter to use `http` protocol by default ([#&#8203;493](`axios/axios#493))
- Fixing proxy issues ([#&#8203;491](`axios/axios#491))

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).
@sajidkhaki
Copy link

sajidkhaki commented Sep 18, 2019

axios.delete({
url: '/system/ABC/delete/' + selectedId,
headers: {'Authorization': 'Bearer ' + token},
withCredentials: true
}).then(function (response) {.....................

i try to use csrf in delete , but i am getting this error in node.js

DELETE http://localhost:3131/system/ABC/delete/473TL095G3x-HpugwipBP0RAMmyer5cPDafrNg0 403 (Forbidden)

@vipulDessai
Copy link

vipulDessai commented Jan 26, 2020

yes, even I'm facing the same issue if I'm using the axios.delete with config withCredentials: true and CORS enabled on the backend server with a different domain,

All though I'm explicitly setting the cookies in response headers which I can see in network tab, these are not sent later when I make axios.delete request

axios.delete(
	process.env.GATSBY_URL_DELETE_FILE, 
	{
		data: {
			fileName,
			jwt,
		},
	},
	{ withCredentials: true }
)
.then(res => {
	return res.text()
})
.catch(error => {
	// handle error from aws lambda
	console.log(error)
})

Note: also checked with JS native XMLHttpRequest, it's not supported to set xhr.withCredentials = true; with CORS, so its obvious that it's not supported in Axios

@axios axios locked and limited conversation to collaborators May 22, 2020
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

8 participants