mirror of
https://gitlab.com/eql/EQL5.git
synced 2025-12-17 07:40:37 -08:00
add example of using Qt for a POST request to a cgi script
This commit is contained in:
parent
6804baef18
commit
2d0aaf6c06
1 changed files with 50 additions and 0 deletions
50
examples/X-extras/server-request.lisp
Normal file
50
examples/X-extras/server-request.lisp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
;;; this is just a snippet to show how to use Qt for POST requests to
|
||||
;;; a cgi script (using e.g. python for cgi)
|
||||
;;;
|
||||
;;; this is here to have an alternative to Lisp solutions, which don't
|
||||
;;; work well in ECL on some platforms (namely Windows)
|
||||
;;;
|
||||
;;; example usage:
|
||||
;;;
|
||||
;;; (server-request "booking.py"
|
||||
;;; (list (cons "from" "2020-08-01")
|
||||
;;; (cons "to "2020-08-08")))
|
||||
|
||||
(qrequire :network)
|
||||
|
||||
;; for testing, using e.g. python3 -m http.server 8080 --cgi
|
||||
(defparameter *server* "http://localhost:8080/cgi-bin/")
|
||||
|
||||
(defun http-part (parameter)
|
||||
(let ((part (qnew "QHttpPart")))
|
||||
(qlet ((name "QVariant(QString)"
|
||||
(format nil "form-data; name=~S" (car parameter))))
|
||||
(|setHeader| part |QNetworkRequest.ContentDispositionHeader| name)
|
||||
(|setBody| part (x:string-to-bytes (cdr parameter))))
|
||||
part))
|
||||
|
||||
(defun server-request (script &optional parameters)
|
||||
"Make a http POST request to a cgi script, forcing synchronous behaviour,
|
||||
which simplifies things and is preferable in many cases."
|
||||
(start-busy-animation) ; pseudo code
|
||||
(let (response)
|
||||
(qlet ((ev-loop "QEventLoop")
|
||||
(manager "QNetworkAccessManager"))
|
||||
(qconnect manager "finished(QNetworkReply*)"
|
||||
(lambda (reply)
|
||||
(|deleteLater| reply)
|
||||
(let ((err (|error| reply)))
|
||||
(when (= |QNetworkReply.NoError| err)
|
||||
(setf response (qfrom-utf8 (|readAll| reply)))))
|
||||
(|exit| ev-loop)))
|
||||
(qlet ((multi "QHttpMultiPart")
|
||||
(qurl "QUrl(QString)" (x:cc *server* script))
|
||||
(request "QNetworkRequest(QUrl)" qurl))
|
||||
(dolist (param parameters)
|
||||
(qlet ((part (http-part param)))
|
||||
(|append| multi part)))
|
||||
(|post| manager request multi)
|
||||
(|exec| ev-loop |QEventLoop.ExcludeUserInputEvents|)
|
||||
(stop-busy-animation) ; pseudo code
|
||||
response))))
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue