# Adding a database record using https function-POST

**URL:** <https://forum.wixstudio.com/t/adding-a-database-record-using-https-function-post/52494>\
**Category:** Show and tell\
**Tags:** code, question\
**Created:** [September 18, 2023, 6:29pm UTC](https://forum.wixstudio.com/t/adding-a-database-record-using-https-function-post/52494 "2023-09-18T18:29:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![erickkipngeno12](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@erickkipngeno12](https://forum.wixstudio.com/u/erickkipngeno12)\
**Post date:** [September 18, 2023, 6:29pm UTC](https://forum.wixstudio.com/t/adding-a-database-record-using-https-function-post/52494/1 "2023-09-18T18:29:24Z")

</div>

I have been trying to add a database record using a http-function POST through a URL. I am getting the error: " Failed to load resource: the server responded with a status of 404 " This is the code in http-functions.js file;

```javascript
import { ok, badRequest,notFound, serverError,created } from 'wix-http-functions';
import wixData from 'wix-data';

export function post_addPayment(request) {
  const data = request.body; // Assuming you send the data as JSON in the request body

  // Define the collection name and data schema
  const collectionName = 'Payments';
  const dataSchema = {
    "phone": data.phone,
    "amount": data.amount
  };

  // Insert the record into the database
  return wixData.insert(collectionName, dataSchema)
    .then(result => {
      return ok({ "body": "Record added successfully" });
    })
    .catch(error => {
      return badRequest({ "body": "Error adding record" });
    });
}

export function get_payments(request){
 let options={
  "headers":{
    "content-Type": "application/json"
  }
};
let auth = {
 "suppressAuth": true,
 "suppressHooks": true
        };
return wixData.query("Payments")
  .find(auth)
  .then( (results) => {
    if(results.items.length>0){
options.body={
  "items":results.items
}
return ok(options);
    }
    options.body ={
     "error": 'Email account ${email} not subscribed'
    };
    return notFound(options)
  } )
  .catch ((error)=>{
    options.body={
"error":error
    };
    return serverError(options)
  })
}
export function post_add(request) {
  let options = {
    "headers": {
      "Content-Type": "application/json"
    }
  };
  // get the request body
  return request.body.json()
  .then( (body) => {
      // insert the item in a collection
      return wixData.insert("Payments", body);
    } )
    .then( (results) => {
      options.body = {
        "inserted": results
      };
      return created(options);
    } )
    // something went wrong
    .catch( (error) => {
      options.body = {
        "error": error
      };
      return serverError(options);
    } );
}

```

The adding url for the above code is;

[https://ericbelyon.wixsite.com/test/\_functions/addPayment/](https://ericbelyon.wixsite.com/test/_functions/addPayment/)

Anyone with an idea how this can be resolved ?

---

<div class="post-metadata">

**Author:** ![Danmax](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/danmax/32/27191_2.png) [@Danmax](https://forum.wixstudio.com/u/Danmax)\
**Post date:** [September 23, 2023, 3:14am UTC](https://forum.wixstudio.com/t/adding-a-database-record-using-https-function-post/52494/2 "2023-09-23T03:14:14Z")

</div>

> [@erickkipngeno12](#):
>
> ```javascript
> import { ok, badRequest,notFound, serverError,created } from 'wix-http-functions';
> import wixData from 'wix-data';
> 
> export function post_addPayment(request) {
> const data = request.body; // Assuming you send the data as JSON in the request body
> 
> // Define the collection name and data schema
> const collectionName = 'Payments';
> const dataSchema = {
> "phone": data.phone,
> "amount": data.amount
> };
> 
> // Insert the record into the database
> return wixData.insert(collectionName, dataSchema)
> .then(result => {
> return ok({ "body": "Record added successfully" });
> })
> .catch(error => {
> return badRequest({ "body": "Error adding record" });
> });
> }
> 
> ```

Did you try something like this?

````javascript
export function post_addPayment(request) {
  let options = {
    "headers": {
      "Content-Type": "application/json"
    }
  };
  const collectionName = 'Payments';
    
     let auth = {
        "suppressAuth": true, // Setting this option to true will bypass the permissions.
        
    }
  // get the request body
  return request.body.json()
  .then( (body) => {
      console.log(body)
      // insert the item in a collection
      return wixData.insert(collectionName, body, auth);
    } )
    .then( (results) => {
      options.body = {
        "inserted": results
      };
      return created(options);
    } )
    // something went wrong
    .catch( (error) => {
      options.body = {
        "error": error
      };
      return serverError(options);
    } );
}```
````
