Modify http headers before do request

Hello there!

I try to add my custom header for any request to my API like this

this.$f7.request.setup({
  beforeSend: async (xhr) => {
    xhr.requestParameters.headers['X-HEADER'] = 'X-VALUE';
    return xhr;
  },
});

But it not work :confused:
I can’t see my header in request

Так в доке же прямо пример:

Framework7.request.setup({
  headers: {
    'Authorization': 'sometokenvalue'
  }
})

Это слишком просто (сейчас у меня так и работает).

Я это использую для так же для авторизации, но ключ у меня временный и быстро протухает. Хочу при каждом запросе устанавливать актуальный.

По документации и мой способ должен работать, но не работает. Баг?

A pre-request callback function that will be called after XHR opened and before XHR send. Can be used to modify the XHR object before it is sent. Use this callback to set custom headers, etc.

Так это для запроса и работает, а вы пытаетесь глобально настойки изменить.

Только что проверил для запроса - не работает.

await this.$f7.request.promise({
  url,
  dataType: 'json',
  beforeSend: (xhr) => {
    xhr.requestParameters.headers['X-HEADER'] = 'X-VALUE';
    return xhr;
  },
});

Что не работает? xhr не нужно возвращать

  beforeSend:(xhr) => {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
}
2 Likes

Спасибо! Получилось установить заголовок :+1:

Но с async/await по-прежнему не работает.
Это уже предложение для Владимира.