我正在使用HttParty来处理我的请求,因为我创建了以下自定义类:
class MyRequestClass
include HTTParty
def initialize(base_uri)
self.class.base_uri base_uri
end
def post(endpoint, options, headers)
self.class.post(endpoint, :body => options.to_json, :headers => headers)
end
...
end
从服务模块,我尝试按以下方式进行POST:
def post_method
options = { "tenantId" => "tenantId", "username" => "nn@gmail.com", "password" => "password", "rememberMe" => "1", "timestamp" => Time.now.to_i }
headers = { "Content-Type" => "application/json", "X-NI-signatureType" => "2", "X-NI-signatureHash" => "my_sign_hash", "X-NI-apiKey" => API_KEY, "X-NI-apiVersion" => "1" }
req = RequestParty.new(BASE_URI).post("/user/authenticate", options, headers)
end
但我得到req = nil。
不确定我错过了什么/做错了什么,因为从bash发出的相同请求可以正常工作:
export TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
export URL="http://my-site/user/authenticate"
echo -n $(iconv -c -f UTF-8 -t US-ASCII <<EOF
{
"tenantId": "tenantId",
"username": "nn@gmail.com",
"password": "password",
"rememberMe" : 1,
"timestamp": "${TIMESTAMP}"
}
EOF
) >${BODY}
curl -v --connect-timeout 10 -m 20 -X POST \
-H "Content-Type: application/json" \
-H "X-NI-signatureType: 2" \
-H "X-NI-signatureHash: ${SIG}" \
-H "X-NI-apiKey: ${API_KEY}" \
-H "X-NI-apiVersion: 1" \
-d @${BODY} \
$URL 2>&1
rm ${BODY}
知道我错过了什么吗?