HTTP request v20+ not expanding switch variables (maybe?)

Post Reply
cwswitch
Member
Posts: 99
Joined: Fri Feb 24, 2017 12:25 am

HTTP request v20+ not expanding switch variables (maybe?)

Post by cwswitch »

Hi,

we are able to complete a request with POST a body, custom, set to Hard Coded values of

Code: Select all

{
    "ExternalConsignment": {
        "ConsignmentNo": "34CAW2079918",
        "ConsignmentTrackingCode": "34CAW2079918",
        "ConsignmentTrackingURL": "https://auspost.com.au/mypost/track/#/search",
        "ExternalConsignmentDeliveries": [
            {
                "DeliveryKey": "584659",
                "DeliveryTrackingCode": "34CAW2079918",
                "DeliveryTrackingURL": "https://auspost.com.au/mypost/track/#/search"
            }
        ]
    }
}
but it fails with "Request finish status: Unsupported Media Type; status code: 415; status description: 415; last error: Request failed with status code 415" when we use variables

Code: Select all

{
    "ExternalConsignment": {
        "ConsignmentNo": "[Job.PrivateData:Key="Consignment"]",
        "ConsignmentTrackingCode": "[Job.PrivateData:Key="Consignment"]",
        "ConsignmentTrackingURL": "[Job.PrivateData:Key="URL"]",
        "ExternalConsignmentDeliveries": [
            {
                "DeliveryKey": "[Job.PrivateData:Key="FDKey"]",
                "DeliveryTrackingCode": "[Job.PrivateData:Key="Consignment"]",
                "DeliveryTrackingURL": "[Job.PrivateData:Key="URL"]"
            }
        ]
    }
}
To make the hardcoded version, I highlighted each variable, eg [Job.PrivateData:Key="Consignment"] copied the value that Switch was returning, then pasted it in place of the variable. In theory then Switch can see the value, and the value works.

Other settings are

URL: the URL we need to use and this is the same whether we test with hardcoded or switch variables so should not be a factor
Request Type: POST a body
Body content: Custom
Custom body: as shown in the code snips above
Authentication scheme: None
Parameters:
Headers:
Response: Attach as dataset
Dataset name: HTTPResponse
Dataset model: Automatic
Response headers: Discard
Retry after failed connection: No
Ignore server certificate errors: No
cwswitch
Member
Posts: 99
Joined: Fri Feb 24, 2017 12:25 am

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by cwswitch »

I'm still curious what I was doing wrong, but I have solved it by some workarounds.

Workaround
  • Get all the data as before
  • Inject a half made json that has placeholder values
  • Use the String Replace app to swap the placeholders for the variables
  • Pass the file through the HTTP Request as Post a Body: Input Job
User avatar
JimmyHartington
Advanced member
Posts: 487
Joined: Tue Mar 22, 2011 7:38 am

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by JimmyHartington »

Maybe you could simplify your flow by using the app "Create text file" https://www.enfocus.com/en/appstore/pro ... -text-file

With this you should be able to create the JSON directly with your variables.
cwswitch
Member
Posts: 99
Joined: Fri Feb 24, 2017 12:25 am

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by cwswitch »

Thanks Jimmy. That's a good idea for simplification.
schmitzdp
Member
Posts: 60
Joined: Thu Aug 06, 2020 1:36 pm

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by schmitzdp »

Hi,

I have the same problem. Why is it not possible to insert variables into “Post a body”? :?

I was thinking of injecting a JSON dataset with variables, but I can't find an application that could execute it.

My goal is to keep the original file, add this dataset, and retrieve the HTTP response for the rest of my flow.

Do you have any ideas?

Thanks
User avatar
magnussandstrom
Advanced member
Posts: 526
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by magnussandstrom »

Isn't the problem that hard brackets [ ] is used for both Switch variables and the array in the JSON?

I use this clunky work-a-round:

Using Create text file to create a JSON but for the array brackets I instead use the place-holder-text startarray and stoparray.

After Create text file I use String replace to replace startarray and stoparray to [ and ].

I can now use HTTP Request POST a body, Body content = Input job.


Example:
{
"company": "[Job.PrivateData:Key="myCompany"]",
"name": "[Job.PrivateData:Key="myName"]",
"orderLines": startarray
{
"lineItem": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/item"]",
"lineDescription": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/description"]",
"lineQty": [Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/qty"]
}
stoparray
}
User avatar
magnussandstrom
Advanced member
Posts: 526
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by magnussandstrom »

It would be a neat feature in the HTTP Request app to be able to substitute array brackets with other characters.
User avatar
magnussandstrom
Advanced member
Posts: 526
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request v20+ not expanding switch variables (maybe?)

Post by magnussandstrom »

Freddy shared a helpful workaround to resolve this issue.

Solution:
Insert double square brackets ([[) for the array’s opening bracket (but not at the last bracket). This prevents conflicts with Switch meta-data brackets while still producing valid JSON for the endpoint.

Example:

Code: Select all

{
  "company": "[Job.PrivateData:Key=\"myCompany\"]",
  "name": "[Job.PrivateData:Key=\"myName\"]",
  "orderLines": [[
    {
      "lineItem": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/item"]",
      "lineDescription": "[Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/description"]",
      "lineQty": [Metadata.Text:Dataset="Xml",Model="XML",Path="/order/document/qty"]
    }
  ]
}
Post Reply