Array in REST API request

Hi,
I'm struggling using an array of strings in an POST request.

I have a temporary state (list_attachments) where I push the tokens:
image

I've to use this array in the raw body of the POST request.
I'm sending the array in the additional scope.

send_email_to_store.trigger({
    additionalScope: {
      email_body: richTextEditor_EmailContent[i].value.replaceAll('<p>', '<br>').replaceAll('</p>', ''),
      attachments: list_attachments.value.join()
    }})

The api body is like this:

{    
  "ticket": {        
    "subject": "xxx",         
    "comment": {             
      "html_body": "{{email_body}}",
       "uploads": [{{attachments}}]
    },    

The problem I've is with the quotes for the array values.

{
  "request": {
    "url": "https://xxx.zendesk.com/api/v2/tickets.json",
    "method": "POST",
    "body": "{\"ticket\":{\"subject\":\"xxxx\",\"comment\":{\"html_body\":\"AAAA\",\"uploads\":[\"[\"yKlv5gIxfuSsfXFbizomB9IL3,n3J5aPnnE1wcTugKaJTIruYXz\"]},}}",

With this code are missing the double quotes in the middle.

Changing the join as:

list_attachments.value.join('","')

still doesn't work, because is adding \ in front of them:

\"uploads\":[\"rDr7o2okWiQ4N5HEOK8rQxlZ7\\\",\\\"G5wdwWwSozaFul5iRRVX7KZ8M\"]}

If I change the body request hardcoding the array, it's working properly.

{    
  "ticket": {        
    "subject": "{{xxx}}",         
    "comment": {             
      "html_body": "{{email_body}}",
       "uploads": ["rDr7o2okWiQ4N5HEOK8rQxlZ7","G5wdwWwSozaFul5iRRVX7KZ8M"]
    }, 

This is how the request appears:

{
  "request": {
    "url": "https://italist.zendesk.com/api/v2/tickets.json",
    "method": "POST",
    "body": "{\"ticket\":{\"subject\":\"xxx\",\"comment\":{\"html_body\":\"AAAA\",\"uploads\":[\"rDr7o2okWiQ4N5HEOK8rQxlZ7\",\"G5wdwWwSozaFul5iRRVX7KZ8M\"]}

What I'm doing wrong?
Thanks

Hello,

Can you try with removing the .join()?

If hardcoded an array works then list_attachments.value is returning an array already - per your first image. The .join() is putting your array into a comma separated list.

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string

source: Array.prototype.join() - JavaScript | MDN

Also after removing .join(), change attachments to as below since attachments is an array object after join is removed.

{    
  "ticket": {        
    "subject": "xxx",         
    "comment": {             
      "html_body": "{{email_body}}",
       "uploads": {{attachments}}
    },    

@lamh_bytecode.io unfortunately seems still not working.

i've changed this:

send_email_to_store.trigger({
    additionalScope: {
      email_body: richTextEditor_EmailContent[i].value.replaceAll('<p>', '<br>').replaceAll('</p>', ''),
      attachments: list_attachments.value
    }})

and

"ticket": {        
    "subject": "xxx",         
    "comment": {             
      "html_body": "{{email_body}}",
       "uploads": {{attachment}}
    },   

but attachment is now null

{
  "request": {
    "url": "https://xxx.zendesk.com/api/v2/tickets.json",
    "method": "POST",
    "body": "{\"ticket\":{\"subject\":\"xxx",\"comment\":{\"html_body\":\"AAA\",\"uploads\":null}

@number15
You missed a s in attachments variable :slight_smile:
image

Ops, you're right sorry.

Still not working, I tried all the following ways:

"uploads": {{attachments}} -> \"uploads\":\"j0g0Aakz4D2wX9R4EAH3ezJ7I,iGz49EcF0DmJT1aBpd5cFLdvS\"

"uploads": "{{attachments}}" -> \"uploads\":\"o6acgW1J0i5mzuosVLEHkCWnq,ty0gmrDWtZcYtPuYAfPZfCIjQ\"

"uploads": ["{{attachments}}"] -> \"uploads\":[\"A99rpY5E8AqCoRdxvsQjxaidO,xzIfHSrRn4IrEx6ZoSTfQcwwZ\"]

"uploads": [{{attachments}}] -> \"uploads\":[\"FABiEZYjy39B8eJJ2ZR5eImj9,zS9jKjRzNEu8KVEuj4CjbW66y\"]

The correct format should be \"uploads\":[\"rDr7o2okWiQ4N5HEOK8rQxlZ7\",\"G5wdwWwSozaFul5iRRVX7KZ8M\"]}

Thanks @number15 for trying, i'll see if I can put something on my end to try it out.

I stood a dummy REST API and used the same two values in your temp state. My original suggestion works for me as the temp state is returning array. Granted, I did not pass the value via addtional scropt but that still should not matters.

image

But if i simply create a text component with {{list_attachments.value}} this is empty, it's not as your state1.

image

The only way I found to print something is like:
image

But this leads to the problem I described above.

I think there is a confusion in communication.

In your initial post, you mentioned you have a temporary state as below. That temporary state contains a array of two string values.
image

Your last message say you can't see it in text component. Text component is meant to display string text. Your list_attachments.value is an array, hence the text component can't display it. The .join essentially creates a string value from array that is why you can see it in text component after added join.

Your REST API, the uploads property is expecting an array (since you said hardcoded array works)...list_attachments.value returns an array. The replication that I did sends in an array object, not string, for the uploads property.

I hope that helps.

1 Like

Wow, don't know why, but refreshing now it's working.
But I have the exact same configuration:

    additionalScope: {
      email_body: richTextEditor_EmailContent[i].value.replaceAll('<p>', '<br>').replaceAll('</p>', ''),
      attachments: list_attachments.value
    }})
{    
  "ticket": {        
    "subject": "xxx",         
    "comment": {             
      "html_body": "{{email_body}}",
       "uploads": {{attachments}}
    },

How is that possible!?

Anyway, good that works :slightly_smiling_face:
Thank you very much!

1 Like

Awesome :smiley:
Glad it works!!