-1

I have a curl request which works

curl -X GET -k 'https://APIADDRESSHERE' -u 'USERNAME:PASSWORD' -H 'Content-Type: application/json'

I can also get this working in ruby:

require 'net/http'
require 'uri'
require 'openssl'

uri = URI.parse("https://APIADDRESSHERE")
request = Net::HTTP::Get.new(uri)
request.basic_auth("USERNAME", "PASSWORD")
request.content_type = "application/json"

req_options = {
  use_ssl: uri.scheme == "https",
  verify_mode: OpenSSL::SSL::VERIFY_NONE,
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

However when I try this with httparty I keep getting a 401 error saying:

An Authentication object was not found in the SecurityContext This request requires HTTP authentication.

I have tried a lot of variations of the following but I'm stuck.

response = HTTParty.get('https://APIURLHERE', {"headers": { "Authorization:" => "BASE64ENCODEDUSERNAMEANDPASSWORD", "Content-Type" => "application/json" }})
Aaron Mills
  • 172
  • 1
  • 14
  • 1
    Possible duplicate of [How to use basic authentication with httparty in a Rails app?](https://stackoverflow.com/questions/7627419/how-to-use-basic-authentication-with-httparty-in-a-rails-app) – anothermh Dec 23 '18 at 00:23
  • It's not a duplicate – Aaron Mills Dec 23 '18 at 00:29
  • Just to be sure, by BASE64ENCODEDUSERNAMEANDPASSWORD you mean Base64 of USER:PASSWORD right? (colon included). – Juan Dec 23 '18 at 00:52
  • How is it not a duplicate? You’re using basic auth with net http and you want to know how to use basic auth with httparty. The error message the _server_ returns even tells you to use basic auth. – anothermh Dec 23 '18 at 01:42
  • I tried using basic auth, but it requires a string, you can see a similar comment on the other one with someone else also stuck using a string – Aaron Mills Dec 27 '18 at 21:15

2 Answers2

1

Have you tried the following?

response = HTTParty.get('https://APIURLHERE', headers: { "Authorization:" => "BASE64ENCODEDUSERNAMEANDPASSWORD", "Content-Type" => "application/json" })
ran
  • 17
  • 2
  • When I do that I get back the error: HTTP Status 401 - An Authentication object was not found in the SecurityContext – Aaron Mills Dec 27 '18 at 21:21
0

Ok I made a simple mistake somewhere along the way because the original solution from the similar post worked... Dont know how it didnt work 50 times last time I was working on this but it was clearly an error on my part somewhere.

Aaron Mills
  • 172
  • 1
  • 14