I am new to Benthos and trying to learn how it works. I am trying to input a JSON file with some key:value pairs, add a new field and POST that new field to get a response.
pipeline:
processors:
- mapping: |
root.id = this.id
root.submitName = "testingAcc"
root.cost = this.cost
let idLen = root.id.length()
let nameLen = root.submitName.length()
root.send = $idLen + root.id + $nameLen + root.submitName + this.cost
- branch:
request_map: |
root = this.send
processors:
- http:
url: "<http link>"
verb: POST
result_map: |
root.response = this.content().string()
Example
Input = {"id": "1", "cost": "500"} Mapping = {"id": "1", "submitName": "testingAcc", "cost": "500", "send": "1110testingAcc500"}
Value that sends via POST = 1110testingAcc500
Branch = {"id": "1", "submitName": "testingAcc", "cost": "500", "send": "1110testingAcc500", "response": "OK"}
However, when I run this I get an error saying that the request_map line (root = this.send) did not find the expected key. I thourght this would work as I mapped the send value so it could be referenced in the branch processor. I have tried different ways to call the keyword (like root = this.send, root = root.send, etc.) but all the same error.
Hopefully I explained this well. I tried googling the error and reading documentation but couldn't find an answer or any examples to help me understand. Any help would be greatly appreciated!
I think you might be seeing this error:
Branch error: request mapping failed: failed assignment (line 1): unable to reference message as structured (with 'this.send'): parse as json: invalid character 'b' looking for beginning of value. The reason is because thethiskeyword mandates that the current in-flight message is in JSON format (if it was in JSON format, but the key didn't exist, you'd get a null value inroot).In your case, I think the
mappingprocessor is failing and then the in-flight message gets passed unchanged to the next processor in the pipeline (thebranchprocessor). You should first ensure that your message is valid JSON by using alogprocessor (if it's not, you can use thecontent()function in amappingprocessor to extract the message data and parse it somehow) and then you need to fix the data type issues in yourmappingprocessor. Bloblang does not allow implicit conversions from integer to string, so you'll need to use$idLen.string()and$nameLen.string()(and maybethis.cost.string()ifcosthappens to be numeric). Try usingbenthos blobl serverto prototype your bloblang mappings.