Code Monkey home page Code Monkey logo

Comments (4)

elbywan avatar elbywan commented on August 25, 2024 4

Hi and thanks for the awesome lib!

Hi @mvasin, thanks!

Is there a way to get response headers when using wretch? I wasn't able to find anything in the docs.

Yes, you can get the headers them by using .res() to retrieve the raw fetch response object.

// see it in action here: https://runkit.com/embed/ydiucqoe45xu

wretch('...').get().res(res => {
    // 'res' is the raw response (see https://developer.mozilla.org/en-US/docs/Web/API/Response)
    // 'res.headers' contains the Headers (https://developer.mozilla.org/en-US/docs/Web/API/Headers)
    console.log(Array.from(res.headers.entries()))
    return res.text()
}).then(console.log)

from wretch.

mrchief avatar mrchief commented on August 25, 2024 1

If you need both headers and the json data, here's how I did it:

.res(response => {
	// do something with response.headers
	return res.json(); // or res.text() if you need text
})
.then(json => {...})

from wretch.

lellky avatar lellky commented on August 25, 2024 1

I had the same question myself and just wanted to att that this works response.headers.get('location')

from wretch.

kuzyo avatar kuzyo commented on August 25, 2024

What is the best way to pass headers further alongside with json data to resolve in then block ?

const successHandler = response => {
    console.log({response})
    if (response.status === 201) {
      let authToken = response.data["auth-token"];
      let { client } = response.data;
      let sessionCookie = response.headers["set-cookie"][0];

      onSuccess(authToken, sessionCookie, client);
    } else {
      errorHandler();
    }
  };

from wretch.

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.