1

I am trying to send a Woocommerce webhook to AWS API Gateway. When i put in my API Gateway URL on Amazon I get the following error:

Error: Delivery URL returned response code: 415

I think this is related to headers, there is an option to Create CORS in API-Gateway that I have now done. Which then adds an OPTION method but I still get undefined in Cloudwatch

enter image description here

I created a POST method and used the mapping template below with application/json and the setting When there are no templates defined (recommended)

{
    "body" : $input.json('$'),
        "headers": {
        #foreach($header in $input.params().header.keySet())
        "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if ($foreach.hasNext), #end

        #end
    }
}

enter image description here

Here is a mini Lambda Node function that just outputs a Woocommerce order number to the console and shows in Cloudwatch whether the API passthrough is working.

exports.handler = (event, context, callback, err) => {
    //callback(null, event.meta_data);
    if (err) console.log('JSON Pass Fail');  // an error occurred
    else console.log(event.order_key); // successful response
};

If anyone did want to have a go at re-creating this, you can knock up a quick Wordpress install on cPanel and install the Woocommerce Plugin. Setup a dummy product and then just use the Cash on Delivery as a payment method to get your "Order Created" webhooks firing. Only takes 2 minutes.

You can use https://requestbin.com/ or webhook.site to test out webhook outputs.

Can anyone help sorting out the Headers so i can pass a Woocommerce payload to API Gateway?

sideshowbarker
  • 72,859
  • 23
  • 167
  • 174
Gracie
  • 806
  • 5
  • 13
  • 32
  • have you also enabled CORS on your function? it needs to be present at both API gateway and in your response – Thales Minussi Mar 20 '19 at 13:55
  • Hi, CORS is enabled. Just wondering whether the header part in the mapping template is needed now that CORS is enabled. Should it just be left as body – Gracie Mar 20 '19 at 14:07
  • I am sorry, maybe I didn't express myself very well. By "enabling" it in the function I meant adding the response headers, like this `const response = { statusCode: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true, }, body: JSON.stringify({ product: product }), };` – Thales Minussi Mar 20 '19 at 14:08
  • I don't have that in my actual Lambda function code no. I'll give that a try. A year or so ago, that code above worked. But perhaps AWS have tweaked the CORS setup somewhere. – Gracie Mar 20 '19 at 14:21
  • All I am looking for is to get the Webhook JSON body data into an event, like I would if if added it to the AWS Lambda test event. – Gracie Mar 20 '19 at 14:23
  • Many people run into this problem. I'd change your code to Node 8, so you can use async await and get rid of callbacks. It should look like: exports.handler = async (event) => { console.log(JSON.stringify(event)); return { statusCode: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true, }, body: JSON.stringify(event) }} Please see if it helps. I have answered pretty much the same question here: https://stackoverflow.com/questions/54805730/serverless-framework-with-aws-cognito-generates-cors-error/54806911#54806911 – Thales Minussi Mar 20 '19 at 15:15

0 Answers0