ruby http请求post方法

ruby http请求post方法,ruby,http,post,request,Ruby,Http,Post,Request,我想拿代币,但我有个问题 我的代码: require 'uri' require 'net/http' url = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password') http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.scheme == 'https') request = Net::HTTP::Post.new(url.path) resp

我想拿代币,但我有个问题

我的代码:

require 'uri'
require 'net/http'
url = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
request = Net::HTTP::Post.new(url.path)
response = http.request(request)
puts response.message
puts response.code
我有如下输出:

Unsupported Media Type
415
API文件

URL /api/authentication/:id/:password Method POST 
URL Params Required: id=[Integer] password=[String] hashed in sha256 
Data 
  Params None 
  Success Response: Code: 200 OK 
    Content: {"authToken":"eyJhbGciOiJIUzI1NiJ9...", "expires": "2017-10-11T12:13:47.441"} 
  Error Response: Code: 401 Unauthorized 
    Content: {"Status":"Wrong Password"} Code: 401 Unauthorized 
    Content: {"Status":"User doesn't exist"}

好了,既然您已经发布了一些信息,我将假设这是一个json API,因为响应是json

更新-请尝试此选项

require 'uri'
require 'net/http'
uri = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
response = http.send_request('POST',uri.request_uri)
由于我没有身份证或密码,我现在得到了一个未经授权的401

如果您喜欢您的原始代码,我已经找到了直接设置请求内容类型的解决方案,例如

require 'uri'
require 'net/http'
url = URI.parse('https://ams.iaau.edu.kg/api/authentication/id/password')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
request = Net::HTTP::Post.new(url.path)
request.content_type = 'application/json' # HERE
response = http.request(request)

你能更具体地说明你的期望吗?不,您只是对给定的URL发出post请求,而没有参数。415表示不支持有效负载格式,但由于POST请求中没有有效负载,因此这并不奇怪。您很可能需要指定一个内容类型标题,例如
application/json
text/xml
,但我不确定这项服务需要什么。我向url添加参数,如身份验证的id和密码。我尝试连接到大学网站,谁向该网站编写api,请告诉我:url=URI(“哈希”)URL/api/authentication/:id/:password方法POST URL参数必需:id=[Integer]password=[String]散列在sha256数据参数中无成功响应:代码:200确定内容:{“authToken”:“eyjhbgcoijiuzi1nij9…”,“expires”:“2017-10-11T12:13:47.441”}错误响应:代码:401未经授权内容:{“状态”:“密码错误”}代码:401未授权内容:{“状态”:“用户不存在”}现在我有:错误的请求,400这很奇怪,我会尝试一些东西,然后再给你回复,因为只要使用curl,我就会得到一个401,有没有内容类型specified@MaratTynarbekov请看更新后的帖子使用这个我现在得到一个401,这是我能得到的没有凭据