尝试用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也没发现类似的错误说明,希望解惑。
囧~~~~瞎了。。。。仔细又看了一遍作者的github,发现加上:content-type :json
就会 Send form params as a json encoded body
不加才是 Send form params as a urlencoded body
于是只要去掉那个就正常了 OTZ