WhatsApp. Object data sent to HTTP Function

Question:
I have WhatsApp business API set up and running. I’m trying to receive messages to a dataset. I have the HTTP function set up and working, it receives data but when logging the (request) it simply says “[object WixHttpFunctionRequestBody]” and doesn’t seem to offer any useable data.

I have set up using glitch.com and that shows the object data as you’d expect. Likewise using webhooks with Make.com (Integromat).

Can anyone help??

//http-function.js 

export function post_whatsApp(request) {

 console.log(request + " post_whatsApp is accessed");

   

/*
{
  "object": "whatsapp_business_account",
  "entry": [
    {
      "id": "0",
      "changes": [
        {
          "field": "messages",
          "value": {
            "messaging_product": "whatsapp",
            "metadata": {
              "display_phone_number": "16505551111",
              "phone_number_id": "123456123"
            },
            "contacts": [
              {
                "profile": {
                  "name": "test user name"
                },
                "wa_id": "16315551181"
              }
            ],
            "messages": [
              {
                "from": "16315551181",
                "id": "ABGGFlA5Fpa",
                "timestamp": "1504902988",
                "type": "text",
                "text": {
                  "body": "this is a text message"
                }
              }
            ]
          }
        }
      ]
    }
  ]
}
*/




 let options = {
 "headers": {
 "Content-Type": "application/json"
        }
    };
 return request.body.json()
    .then( (body) => {
 return createLine(body, options);
    })
    .catch( (error) => {
        options.body = {
 "error": error
        };
 return serverError(options);
    });
    
    
 
}


function createLine(body, options) {
 let dataInsert = {
 "number" : body.number,  
 "title": body.title,
 "message": body.message,
"media" : body.media,

    };
 return wixData.insert('WhatsApp', dataInsert)
    .then( (result) => {
 let item = result;
        options.body = {
 "status": 201,
 "response": item
        };
 return created(options);
    });
}

Assuming the object received in the http function is a JSON object, you will need to convert the JSON object into a JavaScript object to access the data.

This is done using the WixHttpFunctionRequestBody .json() method

Here is a example of what could look like:

import {ok} from 'wix-http-functions';

export async function post_myFunction(request) {
    // Parse the request body into a JSON object
    const json = await request.body.json();

    // Log the JSON object to the console (seen in Site Events log monitor)
    console.log(json);

    // Do whatever is necessary with the object and return a response
    return ok();
}

Note: This example uses async syntax but can be done with promise chaining

Thank you!
It’s so obvious when you see it. :grin: