Address boundary error on POST QHttpMultiPart

Šimon Leitgeb leitgeb.simon at gmail.com
Mon Oct 30 07:16:59 GMT 2023


Hello,
I'm currently working on an application that sends some requests using
PyQt, but multipart formdata post requests are giving me trouble.

Get requests and simple post requests (with just binary data as body) work
without any problems, but requests that contain QHttpMultiPart as body
crash Python with the following:
fish: Job 1, 'pipenv run python post.py' terminated by signal SIGSEGV
(Address boundary error)

I'm using PyQt5 with Python 3.11.5, but the same issue occurs in PyQt6 as
well.
You can find a minimal example below, if anyone would like to test this for
themselves.

Any ideas why this program crashes?

All the best,
Simon Leitgeb

post.py
```python
#!/usr/bin/env python3

import sys
from PyQt5.QtCore import QUrl, QCoreApplication, QByteArray
from PyQt5.QtNetwork import (
    QNetworkAccessManager,
    QNetworkRequest,
    QNetworkReply,
    QHttpPart,
    QHttpMultiPart,
)


class Example:
    def __init__(self):
        self.network_manager = QNetworkAccessManager()
        self.multipart_post()

    def multipart_post(self):
        URL = "http://httpbin.org/post"
        body = QHttpMultiPart(QHttpMultiPart.ContentType.FormDataType)
        boundary = body.boundary()
        part = QHttpPart()
        part.setHeader(
            QNetworkRequest.KnownHeaders.ContentTypeHeader,
"application/json"
        )
        part.setHeader(
            QNetworkRequest.KnownHeaders.ContentDispositionHeader,
            'form-data; name="test"',
        )
        data = QByteArray()
        data.append(b"test")
        part.setBody(data)
        body.append(part)
        request = QNetworkRequest(QUrl(URL))
        request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader,
f'multipart/form-data; boundary={str(boundary, "utf-8")}')
        self.network_manager.finished.connect(self.handle_reply)
        response = self.network_manager.post(request, body)
        print(response)

    def handle_reply(self, reply):
        print(reply.error())
        print(reply.readAll())
        QCoreApplication.quit()


def main():
    app = QCoreApplication([])
    ex = Example()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20231030/77e0da89/attachment.htm>


More information about the PyQt mailing list