MicroPython vs. HTTP Response Headers

Der Raspberry Pi Pico W ist pure Magie. Ein Microcontroller, in Python zu steuern, mit WLAN on board 😍 Super einfach, damit irgendwas im Netz zu machen – essentiell für IoT, looking at you, Arduino.

(Aber man muss|Außerdem kann man) viel über MicroPython lernen. Zum Beispiel, wie urequests Response-Header parst: Als Key/Value. Das ist uncool, da es durchaus Header gibt, die doppelt kommen können und sollen:

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in false as the second argument you can force multiple headers of the same type. For example:

Sagt PHP (replace Parameter).

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

Sagt Mozilla.

Imho nicht unbedingt unseriöse Quellen, aber für MicroPython ist das trotzdem kein schlagendes Argument; Header duplicates werden weiterhin nicht unterstützt werden. Siehe meine längliche Diskussion ab hier.

Es gibt aber einen Workaround, und der ist imho nicht offensichtlich: Man kann nämlich urequests.post() mit einem Parameter parse_headers aufrufen, der erstmal ein Boolean ist – aber auch eine Callback-Funktion sein kann:

Das Fragment (ich hoffe, ich habe auf die Schnelle nichts vergessen) printed alle Cookies. HTH.