-
Update This appears to be a reproducible bug. Go to the second message below for details and instructions to reproduce. End update Hi. I'm working my way through a PR to update the overwrite examples with more info on how you can use the 3 curly brace syntax with a collection variable to update a Request Body with a number, boolean or array value. I've also created a new example that demonstrates how this technique can be used in conjunction with a preRequestScript to modify a numeric collection variable prior to updating an object. I've been using this fairly successfully on my own tests, but in the collection generated using my example configuration with the CRM API, I'm getting an invalid request body. I'm hoping that if I share the configuration here someone may be able to spot my error:. Here is the configuration: $schema: https://raw.githubusercontent.com/apideck-libraries/portman/main/src/utils/portman-config-schema.json
version: 1
globals:
stripResponseExamples: true
variableCasing: snakeCase
assignVariables:
# After creating a new object, save it's ID for later use
- openApiOperation: POST::*
collectionVariables:
- responseBodyProp: data.id
name: "<tag>Id"
# After getting a list of objects, save the ID of the first object returned
# in a postman environment variable
- openApiOperation: GET::*
excludeForOperations: # don't update env. var on a GET request for one object
- "GET::/*/{id}"
collectionVariables:
- responseBodyProp: data[0].id
name: <tag>Id
# After getting an existing lead save it's monetary amount for later use
- openApiOperation: GET::/crm/leads/{id}
collectionVariables:
- responseBodyProp: monetary_amount
name: <tag>MonetaryAmount
overwrites:
- openApiOperation: "*::/crm/*/{id}"
excludeForOperations:
- POST::*
overwriteRequestPathVariables:
- key: id
value: "{{<tag>Id}}"
overwrite: true
# When updating a lead use the Monetary Amount environment variable
- openApiOperation: "PATCH::/crm/leads/{id}"
overwriteRequestBody:
- key: monetary_amount
value: '{{{<tag>MonetaryAmount}}}'
overwrite: true
operationPreRequestScripts:
# Before updating a lead, increase the saved monetary_amount
- openApiOperation: "PATCH::/crm/leads/{id}"
scripts:
- file:increaseMonetaryAmount.js
And here is a partial bit of the request body that is being generated for the PATCH /crm/leads request: {
"name": "Elon Musk",
"company_name": "Spacex",
"owner_id": "54321",
"company_id": "2",
"contact_id": "2",
"first_name": "Elon",
"last_name": "Musk",
"description": "A thinker",
"prefix": "Sir",
"title": "CEO",
"status": "New",
"monetary_amount": "{{leads_monetary_amount}},
"currency": "USD",
...
} Note that there is a double quote before the {{leads_monetary_amount}} value, but no closing quote, resulting in an invalid JSON object. Because I used the 3 curly brace syntax in the config: overwriteRequestBody:
- key: monetary_amount
value: '{{{<tag>MonetaryAmount}}}'
overwrite: true I had expected there to be NO quotes in the value for the monetary_amount field. Can anyone spot my error? Thanks! For completeness, here is the content of the inserted preRequest script, although I don't think it has any bearing on the generated request body. // Pre-request script to check the existing monetary_amount of the lead and then double it
let monetaryAmount = pm.collectionVariables.get("leads_monetary_amount")
if (monetaryAmount > 0) {
monetaryAmount *= 2;
} else {
monetaryAmount = 25000;
}
// Update the environment variable with the new amount prior to updating the lead
pm.collectionVariables.set("leads_monetary_amount", monetaryAmount); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I took a look at this again this morning with less bleary eyes. When the collection variable specified in the overwrites:
# When updating a lead use the Monetary Amount environment variable
- openApiOperation: "PATCH::/crm/leads/{id}"
overwriteRequestBody:
- key: monetary_amount
value: '{{{MonetaryAmount}}}' #no template expression
overwrite: true The generated request body is correctly formatted with no extra quote marks, ie: {
"monetary_amount": {{MonetaryAmount}},
"currency": "USD",
} When I comment out the $schema: https://raw.githubusercontent.com/apideck-libraries/portman/main/src/utils/portman-config-schema.json
version: 1
globals:
stripResponseExamples: true
# variableCasing: snakeCase
overwrites:
# When updating a lead use the Monetary Amount environment variable
- openApiOperation: "PATCH::/crm/leads/{id}"
overwriteRequestBody:
- key: monetary_amount
value: '{{{<tag>MonetaryAmount}}}'
overwrite: true I get a request body that includes: {
"monetary_amount": {{LeadsMonetaryAmount}},
"currency": "USD",
} But, if I try a different value for $schema: https://raw.githubusercontent.com/apideck-libraries/portman/main/src/utils/portman-config-schema.json
version: 1
globals:
stripResponseExamples: true
variableCasing: camelCase
overwrites:
# When updating a lead use the Monetary Amount environment variable
- openApiOperation: "PATCH::/crm/leads/{id}"
overwriteRequestBody:
- key: monetary_amount
value: '{{{<tag>MonetaryAmount}}}'
overwrite: true I get a request body that has the dangling quote mark, ie: {
"monetary_amount": "{{leadsMonetaryAmount}},
"currency": "USD",
} So, it appears there may be a bug when To replicate any of these, save any of the example configs in this message to portman-config.crm.yaml and run I hope this is helpful! |
Beta Was this translation helpful? Give feedback.
-
Thank you @thim81 |
Beta Was this translation helpful? Give feedback.
hi @jpjpjp
Thanks to your input and detailed description, I found the cause and created a PR to solve it.
Result in:
BEFORE
AFTER