clojure clj-http post请求带参数问题

新手上路,请多包涵

尝试用clojure的clj-http来模拟http请求
对测试环境的某登录接口发起请求

通过python的requests库测试得知,使用get带上用户名密码即可登录,使用post需额外增加headers中的Referer信息。

如果使用clj-http的get来登录,返回:status 200 "success":true 登录成功

(ns cjtest.req-test (:require [clj-http.client :as http]))
(println (http/get "http://192.168.1.31:8080/login.json"
                    {:query-params {:loginName "15600000002" :password "11111111"}
                     :content-type :json}))

如使用post来登录,则 返回:status 200 但提示:"登录帐号不能为空"

(ns cjtest.req-test (:require [clj-http.client :as http]))
(println (http/post "http://192.168.1.31:8080/login.json"
                    {:form-params {:loginName "15600000002" :password "11111111"}
                     :headers {"Referer" "http://192.168.1.31:8080/login/page"} 
                     :content-type :json}))

同样的参数,使用python的requests来模拟post则可登录成功。

# -*- coding: utf-8 -*-
import requests
url = 'http://192.168.1.31:8080/login.json'
data = {
    'loginName': '15600000002',
    'password': '11111111'
    }
headers = {
    'Referer': 'http://192.168.1.31:8080/login/page'
}
res = requests.post(url, data=data, headers=headers)

又使用clj-http的post函数试了下其他接口,例如:(这次的post请求又是成功的)

(ns cjtest.req-test (:require [clj-http.client :as http]))
(println (http/post "http://192.168.1.31:8080/dynamicValidateCode/send"
                    {:form-params {:module "register" :phone "17800030101"} 
                    :content-type :json}))

因此比较疑惑是不是由于登录post请求中 loginName 字段名是大写才导致的问题?
看了下原作者的github也没发现类似的错误说明,希望解惑。

阅读 7.6k
2 个回答
新手上路,请多包涵

囧~~~~瞎了。。。。仔细又看了一遍作者的github,发现加上:content-type :json
就会 Send form params as a json encoded body
不加才是 Send form params as a urlencoded body
于是只要去掉那个就正常了 OTZ

新手上路,请多包涵

带上cookie会更好吧,还可以把cookie存起来不用每次都登录。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进