The notification from Supabase Edge function indicates that it received no input body

Currently, I am calling a supabase edge function using the code snippet below:

async function getData(plan_data){
    console.log(plan_data)
    console.log(JSON.stringify({plan_data}))
    const { data, error } = await supabase.functions.invoke("create-stripe-checkout",
    {
        body: JSON.stringify({
            plan_data
        }),
    }
    )
    console.log(data, error)

}

Upon inspecting the edge function, I noticed that the request logged "bodyUsed: false", indicating that the edge function believes no value was passed (even though one was passed to the getData function correctly). I have tried tweaking the syntax without success. Is there something I may be overlooking?

EDIT: The edge function code is as follows:

import { serve } from "https://deno.land/@std/http/server.ts"

serve(async (req) => {
  if (req.method === "OPTIONS"){
    return new Response (null, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "apikey, X-Client-Info, Authorization, content-type", 
      }
    })
  }
  console.log(req)
  const { planId } = await req.json()
  console.log(planId)
  return new Response(
    JSON.stringify({ planId }),
    { headers: { 
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "apikey, X-Client-Info, Authorization, content-type", 
    } },
  )
})

https://i.sstatic.net/X5ENx.png

EDIT: I also tested it with supabase's sample code and encountered the same issue.

Answer №1

Supabase SDK handles the json encoding for you, eliminating the need to do it manually.

async function fetchData(plan_data){
        const { data, error } = await supabase.functions.invoke("create-stripe-checkout",
          {
             body: { plan_data },
          }
        )
        console.log(data, error)
      }

To retrieve the data in your Edge Function, follow this pattern:

const { plan_data } = await req.json()
console.log(plan_data)

Answer №2

After encountering a CORS error, I decided to address it by adjusting the headers in the preflight check response for CORS.

For example:

...
if (req.method === "OPTIONS"){
    return new Response(null, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "*", // Updated this line
      }
    })
  }
...

Answer №3

Is it effective to include the content type header?

async function fetchData(plan) {
  console.log(plan)
  console.log(JSON.stringify({ plan }))
  const { result, failure } = await supabase.functions.invoke("create-stripe-item",
  {
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      plan
    }),
  });
  console.log(result, failure)
}

Answer №4

Finally, after struggling with the same issue for days without any progress, I discovered a solution that worked for me:

const { error } = await supabase.functions.invoke("deleteUser", {
        body: JSON.stringify({
          id: selUser.id,
        }),
      });

The crucial step was to wrap the content of the body in JSON.stringify(). Since you have already done this, perhaps you could attempt the following approach:

const { error } = await supabase.functions.invoke("create-stripe-checkout", {
        body: JSON.stringify({
          id: plan_data.planId,
        }),
      });

Then, try to retrieve the id within the edge function using the following code snippet:

const { id } = await req.json();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The retrieval of JSON file using $.GetJson() is unsuccessful from local directory

There is a basic autocomplete program in place. The data entered in the textbox is successfully captured while debugging, however, the GetJson() function fails to retrieve the JSON file, causing the program to malfunction. Here's the relevant code sn ...

Accessing the Parent Method in ReactJS Components

I'm struggling with finding a solution to this issue. I want to trigger the onClickHandler method from the li element within the context of the Wrapper component to handle the click action. Is it feasible to achieve this? class ComponentA extends Com ...

The issue of basic authentication failing to function on Internet Explorer and Chrome, yet operating successfully on Firefox

Here is how my authentication code looks: public class BasicAuthenticationMessageHandler : DelegatingHandler { private const string BasicAuthResponseHeader = "WWW-Authenticate"; private const string BasicAuthResponseHeaderValue = "Basi ...

I'm looking for a way to check and validate three input fields for date, month, and year individually using either jQuery or JavaScript. Can anyone

My form requires input fields for date, month, and year separately. The validation rules are as follows: Date should be between 1-31 and month should be between 1-12. The desired date format is dd mm yyyy. Could someone kindly help me with the HTML code ...

Can I send a DELETE request with request body and headers using Axios?

When working with Axios in ReactJS, I am attempting to send a DELETE request to my server. In order to do this, I need to include the following headers: headers: { 'Authorization': ... } The body of the request consists of: var payload = { ...

When the browser is not in the foreground, clicking on the Bootstrap datepicker with Selenium does not register

When you click on the input field <input id="dp1" class="span2" type="text" value="02-16-2012"> If the browser is in the background, the datepicker popup will not display. Even using javascript or jquery to click the input field does not show the ...

jQuery scrolling table

After struggling with this code for the past 48 hours, I'm reaching out for some guidance. Can someone help point me in the right direction? The task at hand is to create a table that functions similarly to a jQuery calendar. Clicking the next button ...

Tips for sharing data externally using a Typescript Vue component

Struggling with adjusting data from outside the Vue app when using TypeScript with single file components. In my Main.ts file, where I declare a new Vue instance, attempting to allocate it to a variable is not giving me access to that variable. Even using ...

A Guide to Retrieving Parameters and Request Body using Express and Typescript

When I use the PUT method, I encounter this issue: const createFaceList = (req: Request<{faceListId : string}>, res: Response, next: NextFunction) => { console.log(req.body.name); console.log("faceListID = " + req.params.faceListId); a ...

Is it possible for me to modify or add a file within the directory of my HTML file?

Recently, I've been faced with an interesting challenge: I have an HTML file stored in a specific directory on my computer. Whenever I view this HTML file in a web browser, I wish to have the ability to click a button that allows me to either write ...

What method can be used to keep Chromium open while using Puppeteer?

I am trying to figure out how to launch only one instance of Chromium from my first script and then connect to it from subsequent scripts. I am aware of the puppeteer.connect() method, but the issue is that when I start the script responsible for launching ...

Issue with undefined object reference in Moment.js when making an ajax request

Currently, I am working on developing a straightforward booking calendar using Eonasdan's bootstrap-datetimepicker, which relies on the moment.js library. To incorporate localization, I have included the necessary file. My setup consists of linked pic ...

Identify and monitor changes in every element within an array using Typescript/Angular 2

I am working with a person array that I have displayed in a Primeng datatable. Each object in the array has fields for first name, last name, and age, which are represented as columns in the table. Additionally, there is a column to display the status of e ...

Exploring the Method of Accessing data-* Attributes in Vue.js

I have a button that, when clicked, triggers a modal to open. The content displayed in the modal is determined by the data attributes passed to the button. Here is my button: <button class="btn btn-info" data-toggle="modal" data-t ...

Effect of Active or Focus on the HTML <ul> element

Check out this example of a UL tag: <ul style="list-style-type:none; padding:5px ; border:solid 1px #666666;"> <li>I am waiting </li> <li>I am waiting </li> <li>I am waiting </li> <li>I am wa ...

Utilizing NodeJS to generate a payment intent through Stripe

While attempting to process a payment using Stripe, I encountered an issue. Even though the payment was successful and a success response was displayed on the browser, it did not reflect in the dashboard. const express = require("express"); const ...

Synchronizing subscriptions

Currently, I am engaged in a project for my organization and am strictly upholding the Non-Disclosure Agreement (NDA) that I have signed. In adherence to this confidentiality agreement, I present the following code snippet devoid of any sensitive details: ...

Determine whether a WebElement contains a particular content within the :after pseudo class

After locating my element in Selenium, I've come across an interesting challenge. IWebElement icon = box.FindElement(By.ClassName("box-icon")); Sometimes, this element (icon) has a content set as follows: &:after { content: $icon-specia ...

React.js error: Uncaught TypeError: Cannot read property 'map' of null

Hey there, I'm currently working on creating a MERN Stack web app following this tutorial: https://youtu.be/aibtHnbeuio. I'm fairly new to the stack and could really use some help fixing this issue... Error Encountered: TypeError: Cannot read p ...

Employing jQuery to redirect to a different URL when a button is clicked

I've been experimenting with a project that involves both JQuery and AJAX. One of the features I have added is JQuery autofill. Here is the code snippet for the form: <form class="form-horizontal"> <div class="form-group"> < ...