Problem with http-function post()

Hi
I have a problem with the POST method for receiving data after the transaction. I use Tpay online bank for payments, and it gives notification as POST array parameters to the server.
But when I use this code:

export function put_myFunction(request) {
  let options = {
    "headers": {
      "Content-Type": "application/json"
    }
  };
  // get the request body
  return request.body.json()
  .then( (body) => {
      // update the item in a collection
      return wixData.update("myCollection", body);
    } )
    .then( (results) => {
      options.body = {
        "inserted": results
      };
      return ok(options);
    } )
    // something went wrong
    .catch( (error) => {
      options.body = {
        "error": error
      };
      return serverError(options);
    } );
}

I get this error from Tpay:

{"ERROR":{"name":"JsonSyntaxError","errorGroup":"User"}}

Tpay has some code example on PHP:

// Check IP address and POST parameters
   $ipTable = array('195.149.229.109', '148.251.96.163', '178.32.201.77',
   '46.248.167.59', '46.29.19.106', '176.119.38.175');

   if (in_array($_SERVER['REMOTE_ADDR'], $ipTable) && !empty($_POST)) {

        $sellerID = $_POST['id'];
        $transactionStatus = $_POST['tr_status'];
        ...
        $md5sum = $_POST['md5sum'];

        // check transaction status
        if ($transactionStatus=='TRUE' && $error=='none') {
            /* Stuff */
            if ($allOk) {
                /* Stuff */
                echo 'TRUE';
            } else {
                echo 'FALSE - ...'; // describe your error
            }
        } else {
            // Transaction processed with error but handled by merchant system
            echo 'TRUE';
        }
   } else {
        echo 'FALSE - Invalid request';
   }

I guess the problem might be because Tpay sends Array instead of Json, but I can’t find any example on Corvid to process it. I will be thankful for any help.

Hi,

I found the same problem integrating with another server. It seems to be cause server sends bodyRaw info. I solved creating a function to extract from any String the data of a specific element, and passing request.body.text() to it.

The code was:

let text = await request.body.text();
 let json = extractJsonFromString(text, "body");
function extractJsonFromString(string, toExtractObjectName) {
 let jsonAsString = string;
 let jsonStartString = '"' + toExtractObjectName + '":';
 let startPosition = jsonAsString.indexOf(jsonStartString) + jsonStartString.length + 1;
 let endPosition = startPosition + 1;
 let bracketCounter = 1;
 while (bracketCounter > 0 && endPosition < jsonAsString.length) {
 if (jsonAsString[endPosition] === "{") { bracketCounter++ }
 if (jsonAsString[endPosition] === "}") { bracketCounter-- }
        endPosition++;
    }

    jsonAsString = jsonAsString.substring(startPosition, endPosition);

 let json = JSON.parse(jsonAsString);
 return json;
}

Here is my solution for it. Ugly with splits and replaces, but still works with POST form-data without any package.

const boundary = request.headers["content-type"].split('=')[1];
const text = await request.body.text();
const jsonData = getPostData(text, boundary);
function getPostData(body, boundary) {
    const dataItemRaw = body.split(boundary).filter(item => { return item.indexOf('form-data') !== -1 });
    const formattedData = dataItemRaw.map(item => { return item.replace("\r\nContent-Disposition: form-data; ", "").replace("\r\n\r\n", ":").replace("\r\n--", "").replace('name=\"', '').replace('\\"', '') });
    const parsedData = formattedData.map(item => { return item.split('\":') });
    const data = {}
    parsedData.forEach(itemData => {
        data[itemData[0]] = itemData[1];
    });
    return data;
}