Search code examples
ruby-on-railsrubycurlcloud9-idehttp-status-code-302

"HTTP 302 Moved Temporarily" when CURL on Cloud9 using Rails


I have a simple APP that will take requests to create events. It should take an request using an URL and check RegisteredApplication, if it's found, it should create an event linked to that RegisteredApplication.

The problem is that, it seems it's not working on Cloud9! This is the response I've got when doing the Curl command:

* Hostname was NOT found in DNS cache
*   Trying 104.154.33.155...
* Connected to blocmetrics-klaha1.c9.io (104.154.33.155) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-SHA384
* Server certificate:
*        subject: OU=Domain Control Validated; OU=EssentialSSL Wildcard; CN=*.c9.io
*        start date: 2015-03-17 00:00:00 GMT
*        expire date: 2016-05-03 23:59:59 GMT
*        subjectAltName: blocmetrics-klaha1.c9.io matched
*        issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
*        SSL certificate verify ok.
> POST /api/events HTTP/1.1
> User-Agent: curl/7.35.0
> Host: blocmetrics-klaha1.c9.io
> Accept: application/json
> Origin: keeblerheaney.net
> Content-Type: application/json
> Content-Length: 17
> 
* upload completely sent off: 17 out of 17 bytes
< HTTP/1.1 302 Moved Temporarily
< Location: https://c9.io/api/nc/auth?response_type=token&client_id=proxy&redirect=http%3A%2F%2Fblocmetrics-klaha1.c9.io%2Fapi%2Fevents
< Date: Thu, 14 May 2015 21:15:55 GMT
< Transfer-Encoding: chunked
< 
* Connection #0 to host blocmetrics-klaha1.c9.io left intact

This is my routes.rb

namespace :api, defaults: { format: :json } do
  resources :events, only: [:create]
end

This is the api/events_controller.rb

class API::EventsController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    registered_application = RegisteredApplication.find_by(url: request.env['HTTP_ORIGIN'])

    if registered_application.nil?
      render json: "Unregistered application", status: :unprocessable_entity
    end

    @event = registered_application.events.build(event_params)

    if @event.save
      render json: @event, status: :created
    else
      render @event.errors, status: :unprocessable_entity
    end

  end

  private

  def event_params
    params.require(:event).permit(:name)
  end

end

A change I did on config/initializers/inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

And finally, the curl command I'm using.

curl -v -H "Accept: application/json" -H "Origin: keeblerheaney.net" -H "Content-Type: application/json" -X POST -d '{"name":"foobar"}'  https://blocmetrics-klaha1.c9.io/api/events

What am I doing wrong here? Thanks for lending me a hand!


Solution

  • Is your workspace public? Probably not. If you'd like to keep your code private, but your running server accessible by everyone, you can go click on 'Share' within the menu, and check 'public' next to 'Application', which will only make your running server public without exposing your source.