Message#
Superclasses: Object
Represents an HTTP message being sent or received.
A Message represents an HTTP message that is being sent or
received.
You would create a Message with new or
new_from_uri, set up its fields appropriately, and send it.
status_code will normally be a Status value, eg,
OK, though of course it might actually be an unknown status
code. reason_phrase is the actual text returned from the
server, which may or may not correspond to the “standard” description of
status_code. At any rate, it is almost certainly not localized, and not very
descriptive even if it is in the user’s language; you should not use
reason_phrase in user-visible messages. Rather, you should
look at status_code, and determine an end-user-appropriate
message based on that and on what you were trying to do.
Note that libsoup’s terminology here does not quite match the HTTP
specification: in RFC 2616, an “HTTP-message” is either a Request, or a
Response. In libsoup, a Message combines both the request and the
response.
Constructors#
- class Message
- classmethod new(method: str, uri_string: str) Message | None#
Creates a new empty
Message, which will connect touri.- Parameters:
method – the HTTP method for the created request
uri_string – the destination endpoint (as a string)
- classmethod new_from_encoded_form(method: str, uri_string: str, encoded_form: str) Message | None#
Creates a new
Messageand sets it up to send the givenencoded_formtouriviamethod. Ifmethodis “GET”, it will include the form data intouri's query field, and ifmethodis “POST” or “PUT”, it will be set as request body.This function takes the ownership of
encoded_form, that will be released withfreewhen no longer in use. See alsoform_encode,form_encode_hashandform_encode_datalist.- Parameters:
method – the HTTP method for the created request (GET, POST or PUT)
uri_string – the destination endpoint (as a string)
encoded_form – a encoded form
- classmethod new_from_multipart(uri_string: str, multipart: Multipart) Message | None#
Creates a new
Messageand sets it up to sendmultiparttouri_stringvia POST.- Parameters:
uri_string – the destination endpoint
multipart – a
Multipart
Methods#
- class Message
- add_flags(flags: MessageFlags) None#
Adds
flagsto the set ofmsg's flags.- Parameters:
flags – a set of
MessageFlagsvalues
- disable_feature(feature_type: GType) None#
Disables the actions of
SessionFeatures with the givenfeature_type(or a subclass of that type) onmsg.msgis processed as though the feature(s) hadn’t been added to the session. Eg, passingSOUP_TYPE_CONTENT_SNIFFERforfeature_typewill disable Content-Type sniffing on the message.You must call this before queueing
msgon a session; calling it on a message that has already been queued is undefined. In particular, you cannot call this on a message that is being requeued after a redirect or authentication.- Parameters:
feature_type – the
Typeof aSessionFeature
- get_connection_id() int#
Returns the unique idenfier for the last connection used.
This may be 0 if it was a cached resource or it has not gotten a connection yet.
- get_flags() MessageFlags#
Gets the flags on
msg.
- get_force_http1() bool#
Returns whether HTTP/1 version is currently demanded for the
msgsend.Added in version 3.4.
- get_http_version() HTTPVersion#
Gets the HTTP version of
msg.This is the minimum of the version from the request and the version from the response.
Returns if this message is set as a top level navigation.
Used for same-site policy checks.
- get_metrics() MessageMetrics | None#
Get the
MessageMetricsofmsg.If the flag
COLLECT_METRICSis not enabled formsgthis will returnNone.
- get_priority() MessagePriority#
Retrieves the
MessagePriority.If not set this value defaults to
SOUP_MESSAGE_PRIORITY_NORMAL.
- get_remote_address() SocketAddress | None#
Get the remote
SocketAddressof the connection associated with the message.The returned address can be
Noneif the connection hasn’t been established yet, or the resource was loaded from the disk cache. In case of proxy connections, the remote address returned is aProxyAddress. Ifremote_connectableis set the returned address id for the connection to the session’s remote connectable.
- get_request_headers() MessageHeaders#
Returns the headers sent with the request.
- get_response_headers() MessageHeaders#
Returns the headers recieved with the response.
- get_tls_ciphersuite_name() str#
Gets the name of the TLS ciphersuite negotiated for
msg's connection.
- get_tls_peer_certificate() TlsCertificate | None#
Gets the peer’s
TlsCertificateassociated withmsg's connection.Note that this is not set yet during the emission of
accept_certificatesignal.
- get_tls_peer_certificate_errors() TlsCertificateFlags#
Gets the errors associated with validating
msg's TLS peer certificate. Note that this is not set yet during the emission ofaccept_certificatesignal.
- get_tls_protocol_version() TlsProtocolVersion#
Gets the TLS protocol version negotiated for
msg's connection.If the message connection is not SSL, %G_TLS_PROTOCOL_VERSION_UNKNOWN is returned.
- is_feature_disabled(feature_type: GType) bool#
Get whether
SessionFeatures of the givenfeature_type(or a subclass of that type) are disabled onmsg.See
disable_feature.- Parameters:
feature_type – the
Typeof aSessionFeature
- is_keepalive() bool#
Determines whether or not
msg's connection can be kept alive for further requests after processingmsg.The result is based on the HTTP version, Connection header, etc.
- query_flags(flags: MessageFlags) bool#
Queries if
flagsare present in the set ofmsg's flags.- Parameters:
flags – a set of
MessageFlagsvalues
- remove_flags(flags: MessageFlags) None#
Removes
flagsfrom the set ofmsg's flags.- Parameters:
flags – a set of
MessageFlagsvalues
- set_first_party(first_party: Uri) None#
Sets
first_partyas the main documentUriformsg.For details of when and how this is used refer to the documentation for
CookieJarAcceptPolicy.- Parameters:
first_party – the
Urifor themsg's first party
- set_flags(flags: MessageFlags) None#
Sets the specified flags on
msg.- Parameters:
flags – a set of
MessageFlagsvalues
- set_force_http1(value: bool) None#
Sets whether HTTP/1 version should be used when sending this message. Some connections can still override it, if needed.
Note the value is unset after the message send is finished.
Added in version 3.4.
- Parameters:
value – value to set
- set_is_options_ping(is_options_ping: bool) None#
Set whether
msgis intended to be used to sendOPTIONS *to a server.When set to
True, the path ofuriwill be ignored andmethodset to %SOUP_METHOD_OPTIONS.- Parameters:
is_options_ping – the value to set
Sets whether the current request is a top-level navitation.
See the same-site spec for more information.
- Parameters:
is_top_level_navigation – if
Trueindicate the current request is a top-level navigation
- set_method(method: str) None#
Set
msg's HTTP method tomethod.- Parameters:
method – the value to set
- set_priority(priority: MessagePriority) None#
Sets the priority of a message.
Note that this won’t have any effect unless used before the message is added to the session’s message processing queue.
The message will be placed just before any other previously added message with lower priority (messages with the same priority are processed on a FIFO basis).
Setting priorities does not currently work with synchronous messages because in the synchronous/blocking case, priority ends up being determined semi-randomly by thread scheduling.
- Parameters:
priority – the
MessagePriority
- set_request_body(content_type: str | None, stream: InputStream | None, content_length: int) None#
Set the request body of a
Message.If
content_typeisNoneandstreamis notNonethe Content-Type header will not be changed if present. The request body needs to be set again in casemsgis restarted (in case of redirection or authentication).- Parameters:
content_type – MIME Content-Type of the body, or
Noneif unknownstream – a
InputStreamto read the request body fromcontent_length – the byte length of
streamor -1 if unknown
- set_request_body_from_bytes(content_type: str | None = None, bytes: Bytes | None = None) None#
Set the request body of a
MessagefromBytes.If
content_typeisNoneandbytesis notNonethe Content-Type header will not be changed if present. The request body needs to be set again in casemsgis restarted (in case of redirection or authentication).- Parameters:
content_type – MIME Content-Type of the body, or
Noneif unknownbytes – a
Byteswith the request body data
- set_site_for_cookies(site_for_cookies: Uri | None = None) None#
Sets
site_for_cookiesas the policy URL for same-site cookies formsg.It is either the URL of the top-level document or
Nonedepending on whether the registrable domain of this document’s URL matches the registrable domain of its parent’s/opener’s URL. For the top-level document it is set to the document’s URL.See the same-site spec for more information.
- Parameters:
site_for_cookies – the
Urifor themsg's site for cookies
- set_tls_client_certificate(certificate: TlsCertificate | None = None) None#
Sets the
certificateto be used bymsg's connection when a client certificate is requested during the TLS handshake.You can call this as a response to
request_certificatesignal, or before the connection is started. IfcertificateisNonethe handshake will continue without providing a GTlsCertificate. Note that theTlsCertificateset by this function will be ignored iftls_interactionis notNone.- Parameters:
certificate – the
TlsCertificateto set, orNone
- set_uri(uri: Uri) None#
Sets
msg's URI touri.If
msghas already been sent and you want to re-send it with the new URI, you need to send it again.- Parameters:
uri – the new
Uri
- tls_client_certificate_password_request_complete() None#
Completes a certificate password request.
You must call this as a response to
request_certificate_passwordsignal, to notifymsgthat theTlsPasswordhas already been updated.
Properties#
- class Message
-
- props.flags: MessageFlags#
The type of the None singleton.
- props.http_version: HTTPVersion#
The type of the None singleton.
The type of the None singleton.
- props.priority: MessagePriority#
The type of the None singleton.
- props.remote_address: SocketAddress#
The type of the None singleton.
- props.request_headers: MessageHeaders#
The type of the None singleton.
- props.response_headers: MessageHeaders#
The type of the None singleton.
- props.tls_peer_certificate: TlsCertificate#
The type of the None singleton.
- props.tls_peer_certificate_errors: TlsCertificateFlags#
The type of the None singleton.
- props.tls_protocol_version: TlsProtocolVersion#
The type of the None singleton.
Signals#
- class Message.signals
- accept_certificate(tls_peer_certificate: TlsCertificate, tls_peer_errors: TlsCertificateFlags) bool#
The type of the None singleton.
- Parameters:
tls_peer_certificate – the peer’s
TlsCertificatetls_peer_errors – the tls errors of
tls_certificate
- authenticate(auth: Auth, retrying: bool) bool#
The type of the None singleton.
- Parameters:
auth – the
Authto authenticateretrying –
Trueif this is the second (or later) attempt
- content_sniffed(type: str, params: dict[str, str]) None#
The type of the None singleton.
- Parameters:
type – the content type that we got from sniffing
params – a
HashTablewith the parameters
- got_body_data(chunk_size: int) None#
The type of the None singleton.
Added in version 3.4.
- Parameters:
chunk_size – the number of bytes read
- network_event(event: SocketClientEvent, connection: IOStream) None#
The type of the None singleton.
- Parameters:
event – the network event
connection – the current state of the network connection
- request_certificate(tls_connection: TlsClientConnection) bool#
The type of the None singleton.
- Parameters:
tls_connection – the
TlsClientConnection
- request_certificate_password(tls_password: TlsPassword) bool#
The type of the None singleton.
- Parameters:
tls_password – the
TlsPassword