Giter VIP home page Giter VIP logo

Comments (7)

patcrama avatar patcrama commented on May 31, 2024

could any body help me on this?

from use-http.

alex-cory avatar alex-cory commented on May 31, 2024

Are you seeing the headers in the network tab in devtools for the request?

from use-http.

patcrama avatar patcrama commented on May 31, 2024

Hi Alex, Thanks for replying to my issue, I can see headers but not the one I sent X-XSRF-TOKEN when I used useFetch but I can see this header when I am using Axios. Below images is from axios
image
Below is image is from use-http with useFetch hook.
image

from use-http.

alex-cory avatar alex-cory commented on May 31, 2024

It might be because of caching. Did you try cachePolicy: 'no-cache'?

from use-http.

patcrama avatar patcrama commented on May 31, 2024

Hi Alex,
I tried adding cachePolicy but still same error (403), I am suspecting that use-http is not taking the headers I am passing on, not sure if the options I used is right way to pass the headers to use-http, do you have any example of post request with use-http and headers included?

from use-http.

alex-cory avatar alex-cory commented on May 31, 2024
const { ... } = useFetch('url-or-path', options => {
  options.headers = {
    /* your headers here */
  }
  return options
})

You could also do this. And if your url is set in the provider, you don't need to pass the url.

const { post } = useFetch({
  headers: { ... }
})
post('/route', body)

Or you if you are making multiple different api calls from the single useFetch you could do

const { post } = useFetch({
  interceptors: {
    request({ request }) {
       // if the URL is the exact one you want and method === 'POST', then add the headers to I believe `request.headers`
      return request
    }
  }
})

from use-http.

patcrama avatar patcrama commented on May 31, 2024

Thanks for your the details, I added provider and implemented the global options with provider and that worked good. Thank you so much again. I would call it close this issue as it is answered.

const globalOptions = {
interceptors: {
// every time we make an http request, this will run 1st before the request is made
// url, path and route are supplied to the interceptor
// request options can be modified and must be returned
request: async ({ options}) => {

  if(options.method==='POST'){
    options.headers['X-XSRF-TOKEN']=csrfToken;
    options.headers.Accept='application/json';
    options.headers['Content-Type']='application/json'; 
  } 
  return options
},
// every time we make an http request, before getting the response back, this will run
// response: async ({ response }) => {
//   const res = response
//   if (res.data) res.data = toCamel(res.data)
//   return res
// }

}}
return (

);

from use-http.

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.