14

I am using the standard blog tutorial on integrating api gateway with step functions from here: https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-api-gateway.html

My step function expects the following output:

{
  "my_params": {
     "config": "config_value"
  }
}

the Request body needed to do a post request as mentioned in the blog is:

{
 "input": "{}",
   "name": "MyExecution",
   "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld"
}

I am passing my required input like this:

{
 "input": {
           "my_params": {
             "config": "config_value"
             }
          },
  "name": "MyExecution",
  "stateMachineArn": "my-arn"
}

However, I am continuously getting following error:

{
"__type": "com.amazon.coral.service#SerializationException",
  "Message": "Start of structure or map found where not expected."
}

Can someone tell me what exactly is the problem here? What am I doing wrong here? Quick help appreciated.

Omkar
  • 2,274
  • 6
  • 21
  • 34
  • 1
    I'm experiencing something similar but I don't know the exact solution. My best guess is that the value of "input" is supposed to be a string (notice the blog says `"input": "{}"` rather than `"input": {}`. Try converting your input value to a string and passing that in instead of your map object. – cody Feb 13 '18 at 04:19
  • 1
    @cody I actually found out the solution for that. sorry, couldnt post it before. Yes, you are correct. It needs the input in a stringified manner. I used JSON.stringify to create a string out of JSON... then used it to trigger the API and it worked. – Omkar Feb 13 '18 at 04:27

2 Answers2

22

Use escape character for your parameters as follows

{
 "input": "{
           \"my_params\": {
             \"config\": \"config_value\"
             }
          }",
  "name": "MyExecution",
  "stateMachineArn": "my-arn"
}
Amit Dhara
  • 420
  • 4
  • 11
  • 1
    Thank you very much, I spent a bunch of time looking for this info in AWS docs! – DrCord Nov 20 '20 at 23:33
  • 2
    Something to note is that the Mapping Templates don't like line breaks in your value, so I had to combine all of my escaped JSON into a single line. – Sators Mar 23 '21 at 13:55
  • I wanted to add that you can use [$util Variables](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#util-template-reference) to escape any input json acocrdingly. – Joe Sadoski Jan 06 '23 at 19:27
-1

For the people who faced below issue, when trying to set Data with JSON payload (from console )

Convert the JSON payload to base64 string

Request body e.g

 {
       "Data":  { 
          "name": "Dean",
          "role": "actor"
        },
       "StreamName": "yourstream",
       "Partitionkey": "youPartitionKey"
       }
       }

Error

{
  "__type": "SerializationException",
  "Message": "Start of structure or map found where not expected."
}

Go to Method Execution Panel -> Integration Request(AWS) -> Mapping Templates -> Request body passthrough

Now add template, write the code in the template to convert the JSON payload of the Data key to base64 string

{
    "Data": "$util.base64Encode($input.json('$.Data'))",
....
}

Hope this helps!!! Thanks

Ampati Hareesh
  • 1,852
  • 1
  • 15
  • 20
  • this doesn't work. ` Validation Result: warnings : [], errors : [Invalid content type specified: { "Data": "$util.base64Encode($input.json('$.Data'))", .... }] ` – ChumiestBucket Jun 18 '21 at 19:13
  • @ChumiestBucket can you share your input, the above was written for this error Convert the JSON payload to base64 string, please check the latest docs, as it was written two years ago, also share the link here if possible – Ampati Hareesh Jun 22 '21 at 17:07