3

I'm trying to write an API endpoint for creating Redemptions in my app.

In Rails, my model is such that Redemption has many Items (class_name: RedemptionItems).

Here's my POST Body, following what I assume is the correct JSONAPI Specification (since there is no explicit spec for creating parent and child records in one request).

{
  "data": {
    "type": "redemptions",
    "relationships": {
      "items": {
        "data": [{
            "type": "items",
            "attributes": { "offer_id": "1", "quantity": "2" }
          },
          {
            "type": "items",
            "attributes": { "offer_id": "1", "quantity": "3" }
          },
          {
            "type": "items",
            "attributes": { "offer_id": "123", "quantity": "3" }
          }
        ]
      }
    }
  }
}

I'm using JSONAPI::Resources. I have defined my JSONAPI::Resources thus:

class Platform::Api::Members::RedemptionItemResource < JSONAPI::Resource

  model_name 'Platform::RedemptionItem'

  has_one :redemption

end


class Platform::Api::Members::RedemptionResource < JSONAPI::Resource

  model_name 'Platform::Redemption'

  has_many :items, class_name: 'RedemptionItem'

end

It's currently giving me an inavlid links object error, which doesnt tell me anything on how I must improve my request body.

{
  "errors": [
    {
      "title": "Invalid Links Object",
      "detail": "Data is not a valid Links Object.",
      "code": "115",
      "status": "400"
    }
  ]
}
Martin Verdejo
  • 1,229
  • 2
  • 13
  • 24
  • Late reply here, but the reason this doesn't work is that the gem you are using (jsonapi) doesn't support what you are trying to do. You can only link existing resources. In this case, you'd have to create the redemption first, and then add the items afterwards. – Jesper Lugner Jan 26 '18 at 15:18
  • how to add items afterwards? could you please give a example of that, it would great if could post the json format and URL for the same – Ashwini Mar 28 '18 at 08:54
  • Martin Verdejo how did you solved the issue? I am also facing the same. – Ashwini Mar 28 '18 at 08:54
  • Unfortunately, I haven't found an elegant solution using jsonapi resources. – Martin Verdejo Apr 01 '18 at 06:02

0 Answers0