[PyQt] TypeError: setRawHeader(self, Union[QByteArray, bytes, bytearray], Union[QByteArray, bytes, bytearray]): argument 1 has unexpected type 'str'

Florian Bruhin me at the-compiler.org
Sun May 15 11:05:37 BST 2016


* redstone-cold <redstone-cold at 163.com> [2016-05-15 17:58:24 +0800]:
> For
> 
> void QNetworkRequest::setRawHeader(const QByteArray &headerName, constQByteArray &headerValue)
> (1)
> with PyQt4 , 
> request = QNetworkRequest(QUrl(url))
> userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'
> request.setRawHeader('user-agent',userAgent)
> 
> 
> works well
> 
> 
> while with PyQt5.6, just report error 
> TypeError: setRawHeader(self, Union[QByteArray, bytes, bytearray], Union[QByteArray, bytes, bytearray]): argument 1 has unexpected type 'str'
> 
> 
> (2)
> The code in doc http://doc.qt.io/qt-5/qnetworkrequest.html#setAttribute
> request.setRawHeader(QByteArray("Last-Modified"),QByteArray("Sun, 06 Nov 1994 08:49:37 GMT"));
> just report 
> TypeError: arguments did not match any overloaded call:
>   QByteArray(): too many arguments
>   QByteArray(int, str): argument 1 has unexpected type 'str'
>   QByteArray(Union[QByteArray, bytes, bytearray]): argument 1 has unexpected type 'str'
> 
> 
> Any ideas for these problems ?

For PyQt5 < 5.5 you could pass a python string (i.e. text without an
encoding) for a QByteArray. Now you can't, because PyQt just guessed
an encoding which might be wrong ;)

http://pyqt.sourceforge.net/Docs/PyQt5/incompatibilities.html

Pass a bytes object instead, e.g. by adding a 'b' before your string
in your case:

    request.setRawHeader(b'user-agent', userAgent)

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160515/2dbd3629/attachment.sig>


More information about the PyQt mailing list