Имам просто ПРИЛОЖЕНИЕ, което приема заявки за създаване на събития. Трябва да приеме заявка с помощта на URL адрес и да провери RegisteredApplication, ако бъде намерено, трябва да създаде събитие, свързано с това RegisteredApplication.
Проблемът е, че изглежда не работи на Cloud9! Това е отговорът, който получих, когато изпълнявах командата Curl:
* 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
Това е моят routes.rb
namespace :api, defaults: { format: :json } do
resources :events, only: [:create]
end
Това е 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
Промяна, която направих в config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
И накрая, командата curl, която използвам.
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
Какво правя грешно тук? Благодаря, че ми подаде ръка!