Reputation: 4895
My problem is - Faraday can't connect to url. when i open it in browser http://localhost:3001/api/plans
i get response.
but when i try to connect from rails i get response status 405 and empty response body
url = 'http://localhost:3001/api/plans'
conn = Faraday.new(:url => url) do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
resp = conn.post do |req|
req.headers['Content-Type'] = 'application/json'
req.body = data.to_json
end
logger.info '**********************************'
logger.debug "* Login params: #{data.inspect}"
logger.debug "* Url: #{url} "
logger.debug "* Response: #{resp.inspect} "
logger.debug "* Response body: #{resp.body.inspect} "
logger.info '**********************************'
output of logger is:
**********************************
* Login params: {:username=>"14122460670", :password=>"******", :remember_me=>"on"}
* Url: http://localhost:3001/api/plans
* Response: #<Faraday::Response:0x007f9f5c467528 @env={:method=>:post, :body=>"", :url=>#<URI::HTTP:0x007f9f480da450 URL:http://localhost:3001/v1/sessions>, :request_headers=>{"User-Agent"=>"Faraday v0.8.7", "Content-Type"=>"application/json"}, :parallel_manager=>nil, :request=>{:proxy=>nil}, :ssl=>{}, :status=>405, :response_headers=>{"allow"=>"OPTIONS, GET, HEAD", "content-type"=>"text/plain", "connection"=>"close", "server"=>"thin 1.5.1 codename Straight Razor"}, :response=>#<Faraday::Response:0x007f9f5c467528 ...>}, @on_complete_callbacks=[]>
* Response body: ""
**********************************
Why i can't get a response using Faraday and how may i get it?
Upvotes: 0
Views: 3149
Reputation: 1970
Error code 405 means that http method you are using is not supported. So i think the problem might be that in browser you access this site with GET method and in Faraday it is POST.
Configure Faraday to send a GET request.
resp = conn.get do |req|
...
Upvotes: 2