brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Copy as fetch in the Network panel replays a request from the console

Web-Development
~/tips/tip-network-copy-as-fetch-replay

Network panel > right-click the request > Copy > Copy as fetch

Turn any request a page made into code you can tweak and run again.

When an API call misbehaves, I want to send the same request again with one thing changed. The Network panel can copy any request as a fetch() call with its method, headers and body.

Network panel > right-click the request > Copy > Copy as fetch

fetch("https://example.com/api/items?page=2", {
  "headers": { "accept": "application/json" },
  "method": "GET"
}).then(r => r.json()).then(console.log);

Paste the copied code in the Console of the same page and run it. Because it runs from the page itself, cookies for that site go along in the normal way, so the request behaves much like the original. Change the body or a header and run it again; the new request shows up in the Network panel too. Add the .then(...) part yourself to print the result.

The same Copy menu has Copy as fetch (Node.js) for running it in a script, and Copy as cURL for the terminal.

Be careful where you paste it. The copied code can contain authorization headers or tokens, and those are as good as a password for a while. Look through it before you put it in a chat, a ticket or a bug report, and strip anything secret. Also remember that a replayed POST does the real thing again: it can create a second order or send a second email.

Further reading: Chrome DevTools: Network features reference, MDN: fetch()

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link