HardPro challengePythonJavaScriptTypeScript

Multipart Body Parser

Node.jsHTTPParsing

Parse a simplified multipart body. Body string is split by --<boundary>
delimiters, with a final --<boundary>--. Each part has lines:
Content-Disposition: form-data; name="<name>" then a blank line, then content.

solve(boundary, body) returns { name: content }. Trim final \r\n if present.

Sample tests

Test #1No parts
Input: ["B","--B--"]
Output: {}
Test #2Single field
Input: ["BD","--BD\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nhello\r\n--BD--"]
Output: {"a":"hello"}
Test #3Two fields
Input: ["XYZ","--XYZ\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\n1\r\n--XYZ\r\nContent-Disposition: form-data; name=\"b\"\r\n\r\n2\r\n--XYZ--"]
Output: {"a":"1","b":"2"}
Test #4Empty content
Input: ["B","--B\r\nContent-Disposition: form-data; name=\"x\"\r\n\r\n\r\n--B--"]
Output: {"x":""}
Test #5Multiline content
Input: ["BD","--BD\r\nContent-Disposition: form-data; name=\"msg\"\r\n\r\nline1\r\nline2\r\n--BD--"]
Output: {"msg":"line1\r\nline2"}