Sending fetch request gives an empty body

I am trying to send data to my personal server from wix and the body is empty. For some reason it also changed the method from POST to OPTIONS.

 fetch("websiteurl", {
   "method": "post",
   "headers": {
      "Content-Type": "application/json"
   },
   "body": JSON.stringify({
      "test": "1"
   })
 })

Where do you see that the body is empty and the method got changed? Make sure that you take care of the returned Promise from the fetch .

The Exposing APIs example shows a fetch with a JSON body.

I am seeing this on my http-functions.js file.

This is the code inside of that file

export async function use_myFunction(request) {
    console.log(request.method) -> Returns OPTIONS
    console.log(request.body) -> Returns {}
    
    let options = {
        "headers": {
            "Content-Type": "application/json"
        },
        "body": {
            "thing": true
        }
    }
 
    return ok(options) 
}

On the http-functions file an error says that I should be using “use” instead of “post”.


We’re really not set up to debug complex scenarios, but a few points:

Are you running this against the Live (published) site? Or against the Preview site? In any case, make sure you do Save as that seems to matter.

What is the full URL? (you can just use wix.com instead of your real domain)

When do you get the error to use “use” instead of “post”? During execution? Or is it just flagged in the source code?

Why are you using use_myFunction() instead of post_myFunction? Did you try post?

It seems like a strange issue, but I’m sure that in the end it’ll end up being something simple. Have you tried the two examples that are available? Both are set up to provide a good starting point and they work great.

I have used the tutorials and even after using the same code it nets different results. It says that the method is “OPTIONS” so post will not work with that even though I set the method to post. I am running this on my preview site and I do save every time I change anything. The url I am contacting is " https://username.wixsite.com/website/_functions-dev/myFunction " . The reason I am using “use” instead of “post” is because it errors without it.

This is the error I get when I use “post” instead of “use”

I would like to inspect this and perhaps refer to QA. Please post your site URL.

https://ripullofficial.wixsite.com/website

@axillarystudios Some more thoughts:

I understand that the error you get about using “use” instead of “post” is when you’re loading the site. The error seems problematic as the http-functions.js file is not a web module file (with a .jsw extension).

I can’t vouch for the veracity or method of use for the the request-promise NPM library. I would suggest not to use it until the http-request conundrum has been tucked away.

Seems like there’s an issue with the URL in the fetch. Not sure if it’s the problem, but it’s better to perform the fetch in a backend web module and not in the frontend code.

It might be that the http-functions.js file became corrupted and the system "thinks’ that it’s a web module file. Did you rename this file at one point? Did you rename the web module file (RoviveServer.jsw) at some point?

I have renamed it once but haven’t changed the file extension name.

which file did you rename?

I renamed the http-functions file once. I tried using a different file name to deal with the requests but then realized I needed that specific file name.

@axillarystudios That could be part of the problem.

However (and I can’t believe I didn’t see this before), request.body returns a Promise, so you either need to get the json in .then(), or use an await. I suspect that that’s your issue.

If that’s not it, I would recommend creating a new test site and try it there. It might be that the system still relates to the http-functions file as something different due to the rename.

@yisrael-wix Okay I will look into it! At the moment it is really late for me and I’ll be heading to bed. I’ll check it out when I get up and post an update. Thanks for your help.

Also one more thing do you know why the method changes from POST to OPTIONS?

@yisrael-wix I am getting the same result even if I use await on request.body. I also tried creating a new test site but that nets the same result as well.

I figured out the solution myself. Turns out when you print out request.body it appears to be empty but you have to use .text() to see that there actually is body data. To fix the POST method from changing to a OPTIONS method don’t use “application/json” for your header. Instead stringify the object and put the header as " application/x-www-form-urlencoded ". On the server receiving it just convert the body to json.