[QGIS-Developer] MultiPart post network request stopped working in master
Raymond Nijssen
r.nijssen at terglobo.nl
Sat Jan 22 04:07:34 PST 2022
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)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_kort.gml
Type: application/gml+xml
Size: 2538 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220122/160ebb28/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_validate.py
Type: text/x-python
Size: 213 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220122/160ebb28/attachment.py>
More information about the QGIS-Developer
mailing list