简体   繁体   中英

Rails: How to process incoming POST Request in non-json/custom format?

I have a web service, that receives incoming POST requests from another server. The fields of the request map to a model in my rails application called 'message'.

When I send a JSON POST like

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

The request is processed by the block:

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

And the object is saved successfully.

Unfortunately, the POST requests that I receive from the other web service are non-json and in this format:

content=GUESTTEST&time=2012-08-01+10%3A09%3A03&businessNumber=5555&sessionId=5555CHS1343808543654

How do I write my own route and method to process these requests to also map them to my message model?

Any tipps will be greatly appreciated!

===== Update:

I've solved this by creating the the object based on top level params elements in the controller as follows:

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

end

What would be a more elegant way to achieve this?

You should find all the parameters in the params hash. You can check that by outputting them with inserting this in an appropriate view:

<%= debug(params) if Rails.env.development? %>

although it might be possible to pass them directly to update_attributes, (or Message.new, the line is missing in your code snippet) I would write a custom method that sanitizes them and initializes the new Message from the params hash.

The respond_to method is only concerned with the format that is used to respond to the client when the creation request is finished. So here, when you post to the URI .json , it will respond in a JSON format. The same data posted to only /messages will create the same object, but will respond in HTML (or the set default).

In your case, the POST request that you receive is in parameter format and is the same as the data sent out by an HTML form element, for example. The parameters will be in the params variable, as is already present I am sure.

As the route is not concerned with the format, it shouldn't change either.

You can simulate this POST request with curl like so:

$ curl -d "content=GUESTTEST&time=2012-08-01+10%3A09%3A03&businessNumber=5555&sessionId=5555CHS1343808543654" http://localhost:3000/messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM