【发布时间】:2015-12-10 16:12:24
【问题描述】:
我正在尝试将 curl 请求转换为 ruby。我不明白为什么会这样:
curl -H "Content-Type: application/json" -X POST -d '{"username":"foo","password":"bar"}' https://xxxxxxxx.ws/authenticate
虽然不是这样:
uri = URI('https://xxxxxxxx.ws/authenticate')
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri)
req['Content-Type'] = 'application/json'
req.set_form_data(username: 'foo', password: 'bar')
res = https.request(req)
我得到的回应是:
(byebug) res
#<Net::HTTPBadRequest 400 Bad Request readbody=true>
(byebug) res.body
"{\"error\":\"username needed\"}"
有没有办法检查幕后发生的事情?
【问题讨论】:
-
您的 ruby 正在发送纯 html 表单类型的提交,而 curl 正在发送 json。它们是两种完全不同的数据格式。