How To Pass JSON to httpfunctions.js

I have a JSON file I want to send by CURL to my httpfunction.

curl -X POST ^
-H “Content-Type: application/json” ^
–data-binary @ ^

my JS file is:
export async function post_test(response)
{
}

If the parameters are in the url, then i can find them in “response”, but not if they are from the JSON in CURL.

Question: how do I pass JSON to httpfunctions so i can do batch operations with only one call?

Hi there,

This is how the request should be sent.

const requestPayload = {..} // An object of data

return fetch('https://api.provider.net/..', {
    method: 'POST',
    headers: { 'accept': 'application/json', 'content-type': 'application/json' },
    body: JSON.stringify(requestPayload);
})

This is how you should handle the received JSON.

// backend/http-functions.js

export async function use_function(request) {
    if (requst.method === 'POST' || requst.method === 'PUT') {
        const payload = await request.body.json().catch(e => { return null })
    }
}

Hope this helps~!
Ahmad

Thanks, that did work. Here is my test file. It returns the JSON I send it:

export async function post_sendjson ( request )
{
let sError = “” ;
let payload = await request . body . json ()
. catch ( e => sError = e );

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


response . body  = 
{ 
    "status"  :  "SENDJSON Run Complete" , 
    "error"  :  sError , 
    "params"  :  payload 
} 
**return**  ok ( response ); 

}

and here is my CURL script:

set url=“https://wixsite.com/lbksystest/_functions-dev/sendjson
curl -X POST %url% ^
-H “Content-Type: application/json” ^
–data-binary @sendjson.dat

It takes a long time to find this simple solution!

Thanks a lot! And for your quick reply!

Lawrence