Comments (6)
Hi aliyr, you need to use:
request.headers.get('User-Agent')
instead of request.headers['User-Agent']
from ofetch.
Hi aliyr, you need to use:
request.headers.get('User-Agent')
instead ofrequest.headers['User-Agent']
Thank you @julien-terral, It will do
from ofetch.
@julien-terral I added this in my code and this error happened:
Error: Cannot read properties of undefined (reading 'get')
Also my Editor throws me this error:
TS2339: Property 'headers' does not exist on type 'RequestInfo'. Property 'headers' does not exist on type 'string'.
Any ideas?
from ofetch.
I reproduce the exact same issue by my end :/
I have this error TypeError: Cannot read properties of undefined (reading 'get')
when I'm trying to get headers in Nuxt3 with $fetch.
from ofetch.
I found the solution,
You shouldn't destruct the arguments of the function nor use .get
method.
This is the code that works for me by setting header on data
onRequest(req) {
req.options.headers = {
...(req.options.headers || {}),
'some-header': 'some data'
}
}
from ofetch.
It is written in README that:
onRequest
is called as soon asofetch
is being called, allowing to modify options or just do simple logging.
More accurately, onRequest
is called before actual fetch
function is called.
context.request
eventually becomes the first argument of the fetch
function (usually, it's a URL string), rather than what is called req
object which is available in frameworks like h3 or Express.
This is why you can't use get
method.
So, as @aliyr said, you have to modify context.options
instead, which is passed as second argument to fetch
function, if you want to modify requesting behavior.
Lines 94 to 104 in 5943c5f
from ofetch.
Related Issues (20)
- Ability to retry a request after an error
- feat: support callback for `retry` option
- Support for array in query parameters in the get method HOT 1
- setTimeout controlling the timeout is never cleared on response or error HOT 1
- Pick function HOT 1
- Typed API definition HOT 12
- Response does not contain headers HOT 2
- Receiving server-sent events (SSE) HOT 3
- Failed to upload Mega-sized base64 image HOT 1
- Go-like syntax to handle errors HOT 6
- error.statusText & error.statusMessage are undefined after Nuxt build but are set in dev
- deprecate `params` alias for `query` HOT 1
- the HTTP(S) Agent invalid HOT 7
- Can access to typed data in interceptors HOT 2
- Set cookies HOT 11
- error on Response.blob()
- Multipart image upload not working with fetch but with RapidAPI HOT 4
- Missing types in node export HOT 2
- `body` vs `params` option HOT 1
- Inconsistent headers object returned after a retried request
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ofetch.