Search code examples
mysqljsonspring-data-restpostman

How to post data to the table from POSTMAN client


I am developing a shoppingcart application in spring-data-rest. I am testing my application from POSTMAN client. In my application I have Cart table and Cart_item table. Cart_item table has cart_id field which will get id from Cart. I am able to PUT data to Cart and Cart_item individually. But I am not interested to PUT data individually. Hoe can i send cart data and cart_item data in a single JSON.

I am able to insert into cart table with the following JSON: cart url in POSTMAN: http://localhost:8080/sportsrest/carts

{
  "date": "2015-10-10",
  "totalAmount": 1000,
   "createdAt": "2015-04-06T18:30:00.000+0000",
   "updatedAt": "2015-04-09T18:30:00.000+0000",
   "sport":"http://localhost:8080/sportsrest/sports/1",
   "user":"http://localhost:8080/sportsrest/users/9090909090",
   "tenant": "http://localhost:8080/sportsrest/tenants/2"
  }

For cart_item: http://localhost:8080/sportsrest/cartItems

  {
  "rate": 500,
  "quantity": 2,
  "amount": 1000,
  "createdAt": "2015-04-12T23:40:00.000+0000",
  "updatedAt": "2015-04-14T21:35:20.000+0000",
   "merchandise": "http://localhost:8080/sportsrest/merchandises/10",
  "cart":"http://localhost:8080/sportsrest/carts/901",
  "merchandiseType":"http://localhost:8080/sportsrest/merchandiseTypes/1"
  }

How can i PUT the data into cart and cart_item table from a single JSON request?

I tried following way but cart is being added but cart_item is not created in cart_item table: URL: http://localhost:8080/sportsrest/carts

  {
   "http://localhost:8080/sportsrest/cartItems":
      {
       "rate": 50,
       "quantity": 1,
       "amount": 50,
       "createdAt": "2015-04-12T23:40:00.000+0000",
       "updatedAt": "2015-04-14T21:35:20.000+0000",
       "merchandise": "http://localhost:8080/sportsrest/merchandises/10",
       "merchandiseType":"http://localhost:8080/sportsrest/merchandiseTypes/1"
        },
     "date": "2015-10-10",
     "totalAmount": 99898,
     "createdAt": "2015-04-06T18:30:00.000+0000",
     "updatedAt": "2015-04-09T18:30:00.000+0000",
     "sport":"http://localhost:8080/sportsrest/sports/1",
     "user":"http://localhost:8080/sportsrest/users/9090909090",
     "tenant": "http://localhost:8080/sportsrest/tenants/2"
   }

Can anyone please help me..

Thanks in Advance


Solution

  • Postman is a great tool to talk to APIs, but it won't do what you need to do. It seems that you need more than just building an HTTP request (that's what postman do).

    The approach I see would be:

    • send a PUT to carts API
    • store card_id from the response
    • send a PUT to cart_item using card_id (from the first request)

    There are a number of ways to do this outside of postman, one option that is easy if you are in a linux/mac environment is to use curl/awk/grep