Unix/emacs/elisp

http get request

(defun url-http-get (url arg)
  "GET Request."
  (let (
        (response-string nil)
        (url-request-method "GET")
        (url-request-data
         (mapconcat (lambda (arg)
                      (concat (url-hexify-string (car arg))
                              "="
                              (url-hexify-string (cdr arg))))
                    args
                    "&")))
    (switch-to-buffer
     (url-retrieve-synchronously
      (concat url "?" url-request-data)))
    (goto-char (point-min))
    (re-search-forward "\n\n")
    (setq response-string
          (buffer-substring-no-properties
           (point) (point-max)))
    (kill-buffer (current-buffer))
    response-string))

(url-http-get "http://example.com/get.php" '(("hoge" . "fuga") ("piyo" . "1")))  ;; => "[{\"hoge\":\"fuga\"},{\"piyo\":\"1\"}]"

http post request

(defun url-http-post (url args)
  "POST request."
  (let (
        (response-string nil)
        (url-request-method "POST")
        (url-request-extra-headers
         '(("Content-Type" . "application/x-www-form-urlencoded")))
        (url-request-data
         (mapconcat (lambda (arg)
                      (concat (url-hexify-string (car arg))
                              "="
                              (url-hexify-string (cdr arg))))
                    args
                    "&")))
    (switch-to-buffer
     (url-retrieve-synchronously url))
    (goto-char (point-min))
    (re-search-forward "\n\n")
    (setq response-string
          (buffer-substring-no-properties (point) (point-max)))
    (kill-buffer (current-buffer))
    response-string))

(url-http-post "http://example.com/post.php"  '(("hoge" . "fuga") ("piyo" . "1"))) ;; => "[{\"hoge\":\"fuga\"},{\"piyo\":\"1\"}]"



---
update at 2025/12/23 19:50:42

※注:当サイトは特定環境において確認できた事象のみを記述しています。他の環境での動作は一切保証しません。