mardi 5 mai 2015

Rails API - Accessing JSON Message Body

I like to create an API that adds multiple products from the message body. The JSON would look like below:

{"products":[
    {"name":"playstation"}, 
    {"name":"xbox"}, 
    {"name":"bluray"}
]}

The call would go to:

http://localhost:3000/api/v1/products/AddProducts

I'd like to run through .each item within products and do a .create, something like below:

        def AddProducts             
            *each item*.each do |p|
                Product.Create(p)
            end             
        end

My question is - How do I access the JSON being passed into my controller?

Thanks

Aucun commentaire:

Enregistrer un commentaire