branch: elpa/fedi
commit 8aaf289eb6fa7a561e9f76f822acf9227c239d6b
Author: marty hiatt <martianhiatus [a t] riseup [d o t] net>
Commit: marty hiatt <martianhiatus [a t] riseup [d o t] net>

    handle JSON payloads in http--post
---
 fedi-http.el | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/fedi-http.el b/fedi-http.el
index 914a8621d73..52129821643 100644
--- a/fedi-http.el
+++ b/fedi-http.el
@@ -125,17 +125,25 @@ Used for API form data parameters that take an array."
   (cl-loop for x in array
            collect (cons param-str x)))
 
-(defun fedi-http--post (url &optional params headers unauthenticated-p)
+(defun fedi-http--post (url &optional params headers unauthenticated-p json)
   "POST synchronously to URL, optionally with PARAMS and HEADERS.
-Authorization header is included by default unless UNAUTHENTICATED-P is 
non-nil."
+Authorization header is included by default unless UNAUTHENTICATED-P is 
non-nil.
+JSON means we are posting a JSON payload, so we add headers and json-string 
PARAMS."
   (fedi-http--authorized-request "POST"
-    (let ((url-request-data (when params
-                              (fedi-http--build-params-string params)))
-          (url-request-extra-headers
-           (append url-request-extra-headers ; auth set in macro
-                   (unless (assoc "Content-Type" headers) ; pleroma compat:
-                     '(("Content-Type" . "application/x-www-form-urlencoded")))
-                   headers)))
+    (let* ((url-request-data
+            (when params
+              (if json
+                  (json-encode params)
+                (fedi-http--build-params-string params))))
+           (headers (when json
+                      (append headers
+                              '(("Content-Type" . "application/json")
+                                ("Accept" . "application/json")))))
+           (url-request-extra-headers
+            (append url-request-extra-headers ; auth set in macro
+                    (unless (assoc "Content-Type" headers) ; pleroma compat:
+                      '(("Content-Type" . 
"application/x-www-form-urlencoded")))
+                    headers)))
       (with-temp-buffer
         (fedi-http--url-retrieve-synchronously url)))
     unauthenticated-p))

Reply via email to