2

I want take token,but i have a problems.

My code:

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

I have output like this:

Unsupported Media Type
415

API Doc

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"}
4
  • Can you be more specific as to your expectation? right no you are simply making a post request to the given URL without an parameters. 415 means that the payload format is unsupported but since you have no payload on the POST request this is not that surprising. You will most likely need to specify a Content-Type header e.g application/json or text/xml as well but I am unsure what this service is expecting Commented Nov 21, 2017 at 17:28
  • i add parameters to url, like id and password to auth Commented Nov 21, 2017 at 17:31
  • i try to connect to university web site, and who write api to this site, give me this: url = URI("ams.iaau.edu.kg/api/authentication/13010106044/sha-256 hash") Commented Nov 21, 2017 at 17:33
  • 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"} Commented Nov 21, 2017 at 17:39

1 Answer 1

2

Okay now that you have posted some information I am going to assume this is a json API because the responses are json.

Update Try this instead

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)

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Now I have :Bad request,400
That is very strange I will try a few thinsg and get back to you because just using curl I get a 401 with or without content-type specified
@MaratTynarbekov Please see updated post using this I am now getting a 401 which is as far as I can get without credentials

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.