[QGIS-Developer] MultiPart post network request stopped working in master

Alessandro Pasotti apasotti at gmail.com
Mon Jan 24 02:11:22 PST 2022


Hi Raymond,

can you please file a bug and assign it to me, I'll have a look.



On Sat, Jan 22, 2022 at 1:32 PM Raymond Nijssen <r.nijssen at terglobo.nl>
wrote:

> Dear all,
>
> One of my plugins stopped working in QGIS master, because some of the
> requests seem invalid. I spent quite some time on debugging but cannot
> find the problem.
>
> This script runs in 3.22.3 but not ion 3.23. It uploads an xml file to a
> validation service. It should print: {'successful': True, 'errors': [],
> 'warnings': []}
>
> Anyone a clue? I'm feeling that the best way would be using the
> QgsBlockingNetworkRequest post() function but that does not seem to work
> with a QHttpMultiPart variable.
>
> Thanks!
> Raymond
>
>
>
> import json
>
> manager = QgsNetworkAccessManager.instance()
>
> url = QUrl('https://connect.aerius.nl/api/v7/utility/validate')
>
> request = QNetworkRequest(url)
>
> multi_part = QHttpMultiPart(QHttpMultiPart.FormDataType)
>
> file_parts = []
> gml_fn =
>
> '/home/raymond/terglobo/projecten/aerius/202007_calc_input_plugin/demodata/test_20220121/test_kort.gml'
>
> file_parts.append({'name': 'filePart', 'file_name': gml_fn, 'file_type':
> 'application/gml+xml'})
>
> for fp in file_parts:
>      file = QFile(fp['file_name'])
>      file_part = QHttpPart()
>      file_part.setHeader(QNetworkRequest.ContentTypeHeader,
> QVariant(fp['file_type']))
>      name = fp['name']
>      file_part.setHeader(QNetworkRequest.ContentDispositionHeader,
>          QVariant(f'form-data; name="{name}";
> filename="{QFileInfo(file).fileName()}"'))
>      file.open(QIODevice.ReadOnly)
>      file_part.setBodyDevice(file)
>      file.setParent(multi_part)
>      multi_part.append(file_part)
>
> # This line already fires an invalid request in qgis 3.23:
> reply = manager.post(request, multi_part)
>
>
> # In qgis 3.22 the request is ok. The rest of the code will wait for the
> json
> # reply and display it.
>
>
> # make request blocking
> loop = QEventLoop()
> reply.finished.connect(loop.quit)
> loop.exec_()
>
>
> bstr = reply.readAll()
> result_dict = json.loads(bytes(bstr))
> print(result_dict)
>
>
>
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>


-- 
Alessandro Pasotti
QCooperative:  www.qcooperative.net
ItOpen:   www.itopen.it
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220124/2e7efb68/attachment.html>


More information about the QGIS-Developer mailing list