Sending API POST data as a repeat

Some APIs like stripe take Form POST data and others like Rezdy take JSON POST data, in both cases some of the data that can be sent is array data.
In the case of Stripe you can specify many products using their

Name Value
line_items[0][amount] {{Price}}
line_items[0][name] {{Product}}
line_items[0][quantity] {{Quantity}}

However being a form data post API, im trying to work out how to send an array as input parameters so I land up with.

Name Value
line_items[0][amount] 7
line_items[0][name] Red Widget
line_items[0][quantity] 6
line_items[1][amount] 3
line_items[1][name] Green Widget
line_items[1][quantity] 4
line_items[2][amount] 1
line_items[2][name] Blue Widget
line_items[2][quantity] 3

This type of data that can be manually input, but obviously some customers could have 1 product and others have 10, so it needs to be array type data i assume.

In my case the data comes from a multi database query just before the API POST, is there any way to send an array purely server side like this and get the above type of result?

In the case of JSON POST type APIs I manage to get a little closer but still not quite. So same question there.

Here is Rezdy, it uses POST method and takes a data type of JSON

{
"customer": {
    "firstName": "Hugo",
    "lastName": "Sterin",
    "email": "noreply@rezdy.com",
    "phone": "0282443060"
},
"items": [ 
   {
      "productCode": "PF6B25",
      "startTime": "2014-11-02T22:00:00Z",
      "amount": 200,
      "quantities": [
    {
      "optionLabel": "Adult",
      "value": "3"
    },
    {
      "optionLabel": "Child",
      "value": "1"
    },
    {
      "optionLabel": "Student",
      "value": "1"
    }
      ],
       "participants": [
    {
        "fields": [
        {
            "label": "First Name",
            "value": "Hugo"
        },
        {
            "label": "Last Name",
            "value": "Sterin"
        }
        ]
    },
    {
        "fields": [
        {
            "label": "First Name",
            "value": "Simon"
        },
        {
            "label": "Last Name",
            "value": "Lenoir"
        }
        ]
    }
      ]
   }
],
"fields": [
   {
      "label": "Do you have any dietary requirements?",
      "value": "No, I have no requirements. "
   }
],
"comments": "Special requirements go here",
"resellerComments": "Your Agent voucher/redemption code should go here",
"payments": [
    {
      "type": "CREDITCARD",
      "amount": "200",
      "currency": "USD",
      "date": "2014-11-01T10:26:00Z",
      "label": "Payment processed by RezdyDemoAgent"
    }
  ]
}

I need to use array data for the quantities, and again for the participants, which i actually thought was going to be a little simpler.

I made a multi database query of just the 2 fields needed for quantities, used the alias database method to change the names of the 2 database fields to optionLabel and value and if I use a SetValue step of {{myquery1.values()}} it returns almost exactly what I want like this in the Chrome dev tools pane.

{"optionLabel":"Adult","value":"3"},{"optionLabel":"Child","value":"1"},{"optionLabel":"Student","value":"1"}

So its already identically formatted to what the JSON expects. I add the exact same {{myquery1.values()}} inside my JSON POST API and it returns [object Object],[object Object],[object Object] instead of what the setValue step shows.

I am a little stuck now wondering how I am meant to pass array data to an API if all i have is server side data to work with.

If any of this has made sense, is it possible to do, and can someone guide me a little how.

I know it doesn’t make sense intuitively, but you can just provide an array, inside of double quotes:

{
  "customer": {
    "firstName": "Hugo",
    "lastName": "Sterin",
    "email": "noreply@rezdy.com",
    "phone": "0282443060"
  },
  "items": "{{cars}}"
}

Which will produce:

{"customer":{"firstName":"Hugo","lastName":"Sterin","email":"noreply@rezdy.com","phone":"0282443060"},"items":[{"id":1,"make":"Isuzu","model":"Hombre Space","year":"1998"},{"id":2,"make":"Buick","model":"Terraza","year":"2007"},{"id":3,"make":"Mazda","model":"Miata MX-5","year":"1997"},{"id":4,"make":"Ford","model":"Escort","year":"1993"},{"id":5,"make":"Kia","model":"Sedona","year":"2006"},{"id":6,"make":"Acura","model":"TL","year":"2009"},{"id":7,"make":"Dodge","model":"Stealth","year":"1994"},{"id":8,"make":"GMC","model":"Yukon XL","year":"2006"},{"id":9,"make":"Acura","model":"RL","year":"2011"},{"id":10,"make":"Toyota","model":"Prius","year":"2007"},{"id":11,"make":"Mazda","model":"RX-8","year":"2004"},{"id":12,"make":"Dodge","model":"Omni","year":"1978"},{"id":13,"make":"Chevrolet","model":"Malibu","year":"2000"},{"id":14,"make":"Toyota","model":"Tacoma Xtra","year":"1998"},{"id":15,"make":"Mitsubishi","model":"Pajero","year":"1986"},{"id":16,"make":"Oldsmobile","model":"Silhouette","year":"2001"},{"id":17,"make":"Kia","model":"Optima","year":"2012"},{"id":18,"make":"Chevrolet","model":"Lumina","year":"1999"},{"id":19,"make":"Nissan","model":"Versa","year":"2008"},{"id":20,"make":"Lexus","model":"RX","year":"2011"}]}
1 Like

Building a quick PHP custom module an option? It could just format the data for you from various DB query steps as input.
Then use the output as input for the API call.

Otherwise, you might try to come up with a bit complex custom SQL query which would return JSON string too.

1 Like

You’ll have to build the data as a JSON string first.
(Either all API data for that API call, or just the array/object part you want to construct dynamically, it’s up to you and what is easier to implement.)
Then convert it into an object with myString.parseJson().
Now you can pass it into your API.

You can watch a quick walk through here : http://somup.com/crV1b7oVOv

1 Like

Talk about strange, that works, I did it thinking, ummm, i doubt this will work, and wham, it’s perfect, I don’t understand how, but its perfect.

So just to reiterate replacing


with

"quantities":"{{myquery1.values()}}",
       "participants":

Thank you Ken, I would have never even tried that to be honest, made no sense to me at all.

1 Like

Thanks for the other suggestion @sid and @jeoff75, if I had been using PHP and not Node.js, I may ave done the same idea as suggested.

Now @mebeingken, any ideas on how to pass more than 1 product to Stripe Checkout using input parameters, as this tutorial explained almost exactly how I did my implementation Stripe Checkout Tutorial

If I get this second part working, i will sleep for the first time in a while, lol

Just a heads up incase you didn’t already do this but you can covert the data type from Form to JSON and then make the edits in the JSON data window, it should still work fine with Stripe.

Thinkking out loud to solve the products array, a simple query inside of a repeat followed by a set value step? And then declare as Ken as said previously inside double quote marks?

1 Like

Hmmm, never even tried that, will give it a test, if it works as JSON and not only Form Data, then I can do the same thing I just did and my problems will all be solved, thanks @max_gb, and thank you for that amazing Stripe tutorial as well, it really worked well, only part I never got working was the sha1 hash on the NOW_UTC, for some reason it just throws up errors on my end, thinking something is not working in Wappler possibly with regard to the cryptographic formatters.

1 Like

Strange, SHA1 is still working for me for setting the idempotency key, maybe something changed recently in Wappler or unlikely but something with your local pc config?

It may be something to do with Node.js, I will test it again and double check but the error i was getting was that it returned 0 length or something like that.

So it is actually working, the problem is that with it set with a sha1 hash you can not fetch the API Schema even if you input something inside the variable, so make sure it is only added once you have the correct schema and then it works perfectly.

1 Like

I should probably update the tutorial when I have a spare moment, good catch Paul!