Giter VIP home page Giter VIP logo

Comments (6)

juterral avatar juterral commented on September 14, 2024

Hi aliyr, you need to use:
request.headers.get('User-Agent') instead of request.headers['User-Agent']

from ofetch.

aliyr avatar aliyr commented on September 14, 2024

Hi aliyr, you need to use: request.headers.get('User-Agent') instead of request.headers['User-Agent']

Thank you @julien-terral, It will do

from ofetch.

aliyr avatar aliyr commented on September 14, 2024

@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.

juterral avatar juterral commented on September 14, 2024

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.

aliyr avatar aliyr commented on September 14, 2024

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.

NozomuIkuta avatar NozomuIkuta commented on September 14, 2024

It is written in README that:

onRequest is called as soon as ofetch 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.

ofetch/src/fetch.ts

Lines 94 to 104 in 5943c5f

const $fetchRaw: $Fetch["raw"] = async function $fetchRaw (_request, _options = {}) {
const context: FetchContext = {
request: _request,
options: { ...globalOptions.defaults, ..._options },
response: undefined,
error: undefined
};
if (context.options.onRequest) {
await context.options.onRequest(context);
}

from ofetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.