Http-functions from python

Hi all, I’m using this test code in Python to access a simple database query in Wix:

def check_user_id_in_wix(user_id):
    url = f"https://<my-site>/home/_functions/checkUserId"
    data = {"path": [
        "ASDFHFDFGHDHJDF"
    ],
        "headers": {
            "Accept": "*/*"
        },
        "query": {
            "myKey": "myValue"
        }}

    headers = {"Content-Type": "application/json"}

    try:
        response = requests.post(url, json=data, headers=headers)
        response.raise_for_status()
        response_data = response.json()
        return response_data.get('found', False)

    except requests.exceptions.RequestException as e:
        print(f"Error querying Wix database: {e}")
        return False

This returns error 404. Using the same body in Wix test mode, I get a valid response. It’s my first time interfacing an external app with Wix so I’m lost.

I’m making an assumption here that this “checkUserId” is an http function that you have written on your Wix or Wix Studio site.

In that case, the url structure is incorrect. Take a look a the documentation here as it will depend on if you are accessing the call in a free or premium site as to what the structure must be.

For example, /home/ is not part of a valid path for an http func call.

https://dev.wix.com/docs/velo/api-reference/wix-http-functions/introduction

Hi and thanks for your reply. I changed the URL according to my understanding of how it should be formed, but I still get 404. Below is my Wix function (which works on the tester page in Wix):

import { ok, notFound, serverError } from "wix-http-functions";
import wixData from "wix-data";

export function get_myFunction(request) {
  let options = {
    headers: {
      "Content-Type": "application/json",
    },
  };
  // Query a collection to find matching items.
  return (
    wixData
      .query("NSUsers")
      .eq("id", request.path[0])
      .find()
      .then((results) => {
        // Matching items are found.
        if (results.items.length > 0) {
          options.body = {
            items: results.items,
          };
          return ok(options);
        }
        // No matching items are found.
        options.body = {
          error: `'${request.path[0]}' was not found`,
        };
        return notFound(options);
      })
      // Something went wrong.
      .catch((error) => {
        options.body = {
          error: error,
        };
        return serverError(options);
      })
  );
}

And this is the Python code:

def check_user_id_in_wix(user_id):
    data = {"path": [
        "ASDFHFDFGHDHJDF"
    ],
        "headers": {
            "Accept": "*/*"
        },
        "query": {
            "myKey": "myValue"
        }}
    headers = {"Content-Type": "application/json"}
    url = f"https://<my-site>.wixsite.com/mysite/_functions/myFunction/sub?q=" + json.dumps(data)

    try:
        response = requests.post(url, json=data, headers=headers)
        response.raise_for_status()  # Raise an exception for bad status codes
        print(response)

        response_data = response.json()
        return response_data.get('found', False)

    except requests.exceptions.RequestException as e:
        print(f"Error querying Wix database: {e}")
        return False

I have no idea of what I’m doing wrong.

You need to replace the default values in this url with your values

my-site → your actual site name
myFunction → your actual function

and for the query param, i would test with a hardcoded vlaue first to confirm the url is working

one tip - an easy way to know if its working is to put the url in your browser. Running your pythin code is okay as well but this URL would be accessible by the browser once it’s correct