別のサーバーから着信POST要求を受信するWebサービスがあります。リクエストのフィールドは、「メッセージ」と呼ばれる私のRailsアプリケーションのモデルにマップされます。
次のようなJSONPOSTを送信すると
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d
'{"message":{"content":"GUESTTEST","time":"2012-08-01 10:30:99","businessNumber":"5555","sessionId":"5555CHS1343808543654"}}'
http://localhost:3000/messages.json
リクエストはブロックによって処理されます:
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'Message was successfully created.' }
format.json { render json: @message, status: :created, location: @message }
end
そして、オブジェクトは正常に保存されます。
残念ながら、他のWebサービスから受け取るPOSTリクエストは、json以外であり、次の形式です。
content=GUESTTEST&time=2012-08-01+10%3A09%3A03&businessNumber=5555&sessionId=5555CHS1343808543654
これらのリクエストを処理してメッセージモデルにマッピングするための独自のルートとメソッドを作成するにはどうすればよいですか?
どんなヒントでも大歓迎です!
=====更新:
次のように、コントローラーの最上位のparams要素に基づいてオブジェクトを作成することでこれを解決しました。
def create
@message = Message.new(content: params[:content], command: params[:command], messageId: params[:messageId], msisdn: params[:msisdn], businessNumber: params[:businessNumber], keyword: params[:keyword], operatorCode: params[:operatorCode], sessionId: params[:sessionId], time: params[:time])
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'Message was successfully created.' }
format.json { render json: @message, status: :created, location: @message }
else
format.html { render action: "new" }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
終わり
これを達成するためのよりエレガントな方法は何でしょうか?