ruby http请求发布方法 [英] ruby http request post method

查看:62
本文介绍了ruby http请求发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拿令牌,但是我有问题.

I want take token,but i have a problems.

我的代码:

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.

Okay now that you have posted some information I am going to assume this is a json API because the responses are 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)

由于我没有ID或密码,我现在得到了401 Unauthorized的期望

I am now getting a 401 Unauthorized as expected since I don't have an id or password

如果您希望使用原始代码,我找到了直接设置请求内容类型的解决方案,例如

If you would prefer your original code I have found the solution which is setting the content type of the request directly e.g.

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)

这篇关于ruby http请求发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆