# HttpPost to API using Dictionary

**URL:** <https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532>\
**Category:** Ignition\
**Created:** [March 23, 2022, 2:06pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532 "2022-03-23T14:06:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonB](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@RonB](https://forum.inductiveautomation.com/u/RonB)\
**Post date:** [March 23, 2022, 2:06pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/1 "2022-03-23T14:06:29Z")

</div>

postDictionary is being compiled from two separate lists using zip in a for loop.  
Result is:  
{‘Barcode1’: ‘0190076338816517320100078911220322213609008158’,  
‘Barcode2’: ‘0190076338816517320100079111220322213609008157’, }

**First question** , is there a way to make the Key the same and treat each as a separate key/value pair?

Ie. {“Barcode”: “013…”} , {“Barcode”: “014…”}

* * *

I setup the Keys list to take the string “Barcode” + str(index) for the number of cases generated. (the index runs in a range of 1-51, unless the barcode is == “99999”) When I send it to the API it’s rejected because of the attached number. ie. “Barcode1” instead of “Barcode”.

**API is looking for this structure**

```auto

{
  "Pallet": {
    "PalletNumber": "string",
    "Cases": [
      {
        "Barcode": "string"
      }
    ]
  }
}
This works when posting through the API web interface
      {
        "Barcode": "string"
      }
      ,
      {
        "Barcode": "string"
      }

```

* * *

**Scripting to get everything in place**

```auto
if palletStatus == "OK":
	palletFormat = {"Pallet": {"PalletNumber": apiPalletNumber, "Cases": [postDictionary]}}
	palletFormatJsonEncoded = system.util.jsonEncode(palletFormat)

This is after Json encoding;
{
   "Pallet":{
      "PalletNumber":"example 20 digits",
      "Cases": [{"Barcode1":"example 46 digits",
                       "Barcode3":"example 46 digits",
                       "Barcode2":"example 46 digits",
                       "Barcode4":"example 46 digits"
                      }]
                     }
               }

```

Any help would be greatly appreciated and sorry if this is a bit incoherent. If any clarification is needed, please let me know. The version of Ignition is 7.9 and all of this is being tested through the designers script console at the moment, but will later be added as a gateway script that’s triggered by a plc condition. Initially, I did create this using a for loop and httpPost for each iteration, but instead of a possible 50 different messages being sent at a time we are trying for 1 message with 50 records.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 23, 2022, 2:10pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/2 "2022-03-23T14:10:40Z")

</div>

First, edit your post. Highlight all of your code (just the code). Click the “preformatted text” button. Save the edited post.

Then we can read your code.

---

<div class="post-metadata">

**Author:** ![RonB](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@RonB](https://forum.inductiveautomation.com/u/RonB)\
**Post date:** [March 23, 2022, 2:16pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/3 "2022-03-23T14:16:10Z")

</div>

pturmel I’m sorry, I didn’t see a “preview window.” I was in the process of trying to tone it down… as soon as that monstrosity posted I was looking for an edit button. 🙂

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 23, 2022, 2:16pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/4 "2022-03-23T14:16:34Z")

</div>

> [@RonB](#):
>
> postDictionary is being compiled from two separate lists using zip in a for loop.

What's in those lists ?

---

<div class="post-metadata">

**Author:** ![RonB](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@RonB](https://forum.inductiveautomation.com/u/RonB)\
**Post date:** [March 23, 2022, 2:22pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/5 "2022-03-23T14:22:00Z")

</div>

right now listA is the string “Barcode” + a dynamic value from the index ie. “Barcode1” … “Barcode2”  
listB is a (for each case) 46 digit barcode that’s been converted to a string (pulled from the PLC)

print postDictionary would return ie. “Barcode1”:“0190076338212005320100074111220323213610001162”

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 23, 2022, 2:25pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/6 "2022-03-23T14:25:46Z")

</div>

Why are you using a list with `"Barcode" + dynamic value` if you only want “Barcode” ?

```python
barcodes = ["129083", "5123454", "348957", "1237896"]
barcodes = [
	{
		'Barcode': bc
	} for bc in barcodes
]
#[
# {'Barcode': '129083'}
# {'Barcode': '5123454'}
# {'Barcode': '348957'}
# {'Barcode': '1237896'}
#]

```

---

<div class="post-metadata">

**Author:** ![RonB](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@RonB](https://forum.inductiveautomation.com/u/RonB)\
**Post date:** [March 23, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/7 "2022-03-23T14:36:41Z")

</div>

Ignorance. I just started working with python, json formats, and APIs.  
I wasn’t sure how to pass in one key with multiple values into a dictionary. When I tried I had a mess, so my initial thought was to add an identifier to make each key unique. (not realizing that the json structure would look specifically for the string “Barcode” and anything else would not work.

The swagger API structure looks like this:  
`{ "Pallet": { "PalletNumber": "String", "Cases": [{ "Barcode": "String" }] } }`

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 23, 2022, 2:38pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/8 "2022-03-23T14:38:26Z")

</div>

Well then that should solve your problem.  
What’s question 2 ?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 23, 2022, 2:44pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/9 "2022-03-23T14:44:52Z")

</div>

Not certain but, pretty sure dictionary comprehensions won’t work in 7.9.

May need to refactor to:

```auto
barcodes = ["129083", "5123454", "348957", "1237896"]
tBarcodes = []
for bc in barcodes:
	tBarcodes.append({'Barcode':bc})
barcodes = tBarcodes

```

Could be completely wrong though, don’t have 7.9 to trial it on.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 23, 2022, 2:49pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/10 "2022-03-23T14:49:40Z")

</div>

It’s not a dictionary comprehension, it’s a list comprehension that makes a list of dictionaries. No reason it shouldn’t work.

---

<div class="post-metadata">

**Author:** ![RonB](https://avatars.discourse-cdn.com/v4/letter/r/ce73a5/32.png) [@RonB](https://forum.inductiveautomation.com/u/RonB)\
**Post date:** [March 23, 2022, 2:53pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/11 "2022-03-23T14:53:13Z")

</div>

Can I send you a PM?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 23, 2022, 2:54pm UTC](https://forum.inductiveautomation.com/t/httppost-to-api-using-dictionary/57532/12 "2022-03-23T14:54:41Z")

</div>

Yea sure.
