Send POST request to Wix sends an empty body

Hi everyone,

I’m trying to build a Python script to send a POST request to a function post_myFunction inside my backend file http-functions.js

The request is successful, however it always returns an empty body. My python script is:

import requests
import json

url = “https://www.mitrip.live/_functions-dev/myFunction

headers = {
“Content-Type”: “application/json”,
}

data = {
“tripId”: “123456”,
“destination”: “Paris”,
“startDate”: “2023-10-01”,
“endDate”: “2023-10-10”,
“duration”: 9,
“nbTravellers”: 2,
“vibe”: “Chill”,
“pace”: “Medium”,
“activities”:
}

payload = json.dumps(data)
response = requests.post(url, data=payload, headers=headers)

if response.status_code == 200:
print(response.json())
else:
print(“Error:”, response.status_code, response.text)

My function myFunction is defined as follows:

export async function post_myFunction(request) {
const record = request.body;
// Log the received data for debugging
console.log(“Received data:”, JSON.stringify(record));
// rest of my code

On my logs, I always get [“Received data:”,“{}”]

I’ve tried different variations to send the payload object (ex: removing json.dumps, replacing data=payload with json=payload) but I always get the same result. I have also tried to send this POST request using Postman, but I get the same issue.

If that can help, I have successfully managed to send a GET request with a Python script. The issue really seems to be with the POST.

Would you have any idea on how to correctly send my JSON object to Wix?

Many thanks in advance!

Amine

Hey Amine,

The .body doesn’t work this way. Check through the docs here to see how it should be received, but .body returns a Promise, which is why it’s not showing any results except that empty object. You’ll need to resolve that Promise. You can do this in at least two ways, using .then() or using async/await to resolve it.

Here’s an example using async/await:

export async function post_myFunction(request) {
const record = await request.body.json();
// Log the received data for debugging
console.log(“Received data:”, record);
// rest of my code
}
4 Likes

Hi Chris,

You’re right, thanks a lot for pointing that out, it now works!

1 Like

This saved me! I was using async/await and didn’t realize I had to make that additional call! :slight_smile:

2 Likes