Skip to content

followRedirect re-POSTs on 303/301/302; GET data dropped after a #fragment; form arrays comma-joined #981

Closed

Description

@trackoor

When following a 303/301/302 redirect after a POST, urllib re-sends the request as a POST with the original body; also, GET data is dropped when the URL has a #fragment, and array values in default form bodies are comma-joined instead of repeating the key.

// node repro.mjs (with urllib@4.9.1)
import http from 'node:http';
import { request } from 'urllib';

const server = http.createServer((req, res) => {
  let body = '';
  req.on('data', (c) => (body += c));
  req.on('end', () => {
    if (req.url === '/r/303') {
      res.writeHead(303, { Location: '/echo?from=303' });
      return res.end();
    }
    res.writeHead(200, { 'content-type': 'application/json' });
    res.end(JSON.stringify({ method: req.method, url: req.url, body }));
  });
});

await new Promise((r) => server.listen(0, '127.0.0.1', r));
const base = `http://127.0.0.1:${server.address().port}`;

console.log((await request(`${base}/r/303`, { method: 'POST', data: { a: 1 }, followRedirect: true, dataType: 'json' })).data);
// actual:   { method: 'POST', url: '/echo?from=303', body: 'a=1' }
// expected: { method: 'GET', url: '/echo?from=303', body: '' }

console.log((await request(`${base}/echo#frag`, { data: { a: 1 }, dataType: 'json' })).data);
// actual:   { method: 'GET', url: '/echo', body: '' }
// expected: { method: 'GET', url: '/echo?a=1', body: '' }

console.log((await request(`${base}/echo`, { method: 'POST', data: { b: ['x', 'y'] }, dataType: 'json' })).data);
// actual:   { method: 'POST', url: '/echo', body: 'b=x%2Cy' }
// expected: { method: 'POST', url: '/echo', body: 'b=x&b=y' } (as GET already sends: '?b=x&b=y')

server.close();
  1. Per RFC 9110 §15.4.4 and the Fetch standard, a 303 redirect switches the request to GET without a body (and Fetch/browsers also switch POST to GET on 301/302). #requestInternal instead reuses the original options (method, data, content) for every redirect status instead of keeping the method and body only on 307/308.
  2. In src/HttpClient.ts line 597, the GET query is built as href + (href.includes('?') ? '&' : '?') + query. When the URL has a #fragment, the query string is appended after # and becomes part of the fragment, so it is never sent on the wire.
  3. In src/HttpClient.ts line 618, non-nested form bodies use new URLSearchParams(args.data).toString(), which coerces array values with Array.prototype.toString() ('x,y'), whereas the GET branch uses querystring.stringify(args.data) and repeats the key (b=x&b=y).

Versions: urllib@4.9.1 (latest 4.9.0), and master 99741f8f on Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

  1. fengmk2 commented on Nov 23, 2026

    @fengmk2
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions