Important Update: Some Community URL Redirects are Under Maintenance. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Using REST to create records from a web form

ScottNess3
Collaborator III

I'm working on a new project that will take content from a web form and create an Archer record.  At present, I'm just testing the API using powershell.  I can complete the REST API and create a record using hard coded values in the field value contents i.e. - 

"FieldContents": {
"27356": {
"Type": 1,
"Tag": "backgound",
"Value": "Test background text",
"FieldId": 27356
},

 

My question is for a text field such as above, what is the syntax for the "Value" that will allow for a variable.  I tried something like:

"Value": $background

Where I previously defined $background = "Test background text"

But this is returning a (400) bad request.

 

This is my first API project and I'm using the API Template tool as much as possible to understand basic structure, but need some assistance on this point.

 

TIA

1 ACCEPTED SOLUTION

Accepted Solutions

There are many ways to create/form the $body, just depends on your coding style and preference.  In the code sample provided, the issue is with the $body variable and the use of single ticks/apostrophe.  When using a single tick, variable names in the value will not be replaced and you'll need to use double quotes instead.  For more details, check out about_Quoting_Rules - PowerShell | Microsoft Docs.

 

Try this instead....

$body = @"
{
"Content": {
"LevelId": 393,
"FieldContents": {
"27356": {
"Type": 1,
"Tag": "background",
"Value": $background,
"FieldId": 27356
},
"27354": {
"Type": 1,
"Tag": "unique Title or Name",
"Value": $title,
"FieldId": 27354
}
}
}
}
"@

View solution in original post

6 REPLIES 6

Ilya_Khen
Champion III

This may be close because if I use

"Value": ' + $background + ',

At least the variable text color changes to match the intellisense, but still hitting the 400 error

JeffLetterman
Archer Employee
Archer Employee

I'm curious what the type is for $background.  Try this command to get the type: $background.GetType().

 

Also, try this too: "Value": "$background"

 

The following is for the Contacts application, but the same logic can apply.

$csvFirstName = "John"
$csvLastName = "Doe"

$content = @{
Content = @{
LevelId = 37
FieldContents = @{
"531" = @{Type = 1; Tag = "First Name"; Value = $csvFirstName; FieldId = 531}
"543" = @{Type = 1; Tag = "Last Name"; Value = $csvLastName; FieldId = 543}
}
}
}

$body = $content | ConvertTo-Json -Depth 9‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The field Background is a text field, Type 1.  Placing the variable in double quotes only sent $background as the value.  While that did allow the API to create a record, it does not contain the correct content.

 

This may be my lack of familiarization with the API, but my snippet that does work looks like:

 

try{
$api_url = $base_url + "/platformapi/core/content"
$body =
'{
"Content": {
"LevelId": 393,
"FieldContents": {
"27356": {
"Type": 1,
"Tag": "background",
"Value": "$background",
"FieldId": 27356
},
"27354": {
"Type": 1,
"Tag": "unique Title or Name",
"Value": "$title",
"FieldId": 27354
}

}
}
}'

$results = Invoke-RestMethod -Method POST -Uri $api_url -Body $body -Headers $headers -ContentType "application/json" -WebSession $sess
if ($results.IsSuccessful -and $results.ValidationMessages.count -eq 0) {
$results
$content_id = $results.RequestedObject.Id
}
else {
$results.ValidationMessages
}
}
catch {
$_.Exception|Format-List -Force
}
finally {
$results = $null
}

 

Where the example from Jeff is different.  So am I starting at the wrong point?

There are many ways to create/form the $body, just depends on your coding style and preference.  In the code sample provided, the issue is with the $body variable and the use of single ticks/apostrophe.  When using a single tick, variable names in the value will not be replaced and you'll need to use double quotes instead.  For more details, check out about_Quoting_Rules - PowerShell | Microsoft Docs.

 

Try this instead....

$body = @"
{
"Content": {
"LevelId": 393,
"FieldContents": {
"27356": {
"Type": 1,
"Tag": "background",
"Value": $background,
"FieldId": 27356
},
"27354": {
"Type": 1,
"Tag": "unique Title or Name",
"Value": $title,
"FieldId": 27354
}
}
}
}
"@

Thanks, that helped guide the way.  The end result after testing different variations is:

 

"Value": '$background'

 

Needed the single tick around the variable