{"data":{"id":48,"slug":"working-with-csrf-tokens-part-3-48","title":"Working with CSRF tokens, part 3","excerpt":"For part 3, we revisit the basics with a fresh example and a few lessons learned the hard way. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to…","seriesName":"Working with CSRF tokens","seriesPart":3,"sections":[{"heading":"Introduction","paragraphs":["For part 3, we revisit the basics with a fresh example and a few lessons learned the hard way. Cross-site request forgery tokens are one of the first things that trip up a script trying to log in to a site: the form posts fine in a browser but fails with a 403 from code."]},{"heading":"What to watch for","paragraphs":["Pagination comes in several shapes: numbered pages, next links, offsets and opaque cursors. Each has trade-offs, and a robust scraper should detect the end of the result set rather than assuming a fixed number of pages.","Time zones matter more than they seem. Store timestamps in UTC and convert only when displaying them.","Be careful with relative URLs. Resolve every link against the page it came from, not against the site root, or you will request pages that do not exist."]},{"heading":"Step by step","paragraphs":["Log every request with its URL, status code and duration. When something goes wrong at two in the morning, those logs are the difference between a quick fix and a long investigation.","Keep configuration such as base URLs, delays and selectors out of your code. Changing a delay should not require a redeploy.","Read the site's terms of service before you start. Many sites welcome automated access within limits and say so explicitly; others ask you to use an API instead."]},{"heading":"Key takeaway","paragraphs":["Load the form page with a session that keeps cookies, read the hidden token field, and send it back with the same session in the same request."]}],"authorId":7,"authorName":"Kwame Mensah","authorSlug":"kwame-mensah","category":"Testing","tags":["http","selenium","javascript","playwright"],"publishedAt":"2025-07-22T00:00:00.000Z","wordCount":244,"readingTimeMinutes":2}}