How to update data through API

Keeping data updated in the Faux API tool is essential to ensure the accuracy and relevance of stored information. This process is facilitated through two main methods using the PUT request.

Updating Data by ID:

One way to update data is by specifying the unique identifier (ID) of the data entry you want to modify. This method allows for targeted updates to individual records within the dataset. Developers can precisely specify the ID of the data they want to update, ensuring that only the intended information undergoes modification.

For example, if there's a specific record identified by a particular ID that needs updating, developers can use the following API URL structure:

Incorporate headers like 'Content-Type: application/json' and use JSON.stringify() to format the updated data in the request body. Handle the response with .then(), checking for errors, and apply the updated data in your application. Ensure the URL includes the unique ID, adjusting the parameters accordingly. This method enables the focused updating of data linked to a particular ID on Faux API.

const data = {
   /* Updated data */
}
fetch('serve/<apiName_tokenNo>/<id>', {
      method: 'PUT',
      headers: {
         'Content-Type': 'application/json',
        // Include Authorization headers if required
        'Authorization': 'Bearer YOUR_TOKEN_HERE'
      },
      body: JSON.stringify(data)
   })
   .then(response => response.json())
   .then(data => {
      // Use the retrieved data in your application
      console.log(data);
   })
   .catch(error => {
      // Handle errors if the fetch request fails
      console.error('Error:', error);
   });

Updating Data Using Filters:

A Developers can also perform data updates by providing key-value pairs as filter criteria. This approach enables updating multiple records at once, as it applies changes to all entries that meet the defined conditions.

All Query Parameters (Explained with Defaults)

ParameterTypeDefaultDescription
searchModestring"OR"Select "AND" to match every rule in a row, or "OR" to match any rule from different rows.
key1=vallue1, key2=value2,....string---Use key=value to filter by field. Add more filters like key2=value2 if needed.
globalSearchstring---Type any keyword (e.g., "trainer", "admin") to search across every field. Multiple keywords work too.

🔥Example API: users

idnamerolestatus
1Jadeadminactive
2Maxuseractive
3Ellaadmininactive
4Finnuserinactive
5Ninatrainersinactive

🔹Update Records with OR Matching

You want to update records where any of the following is true:

  • name = Jade

  • OR

  • role = user

  • OR

  • globalSearch = trainer

API URL: serve/<apiName_tokenNo>&name=Jade&role=user&globalSearch=trainer

🟡 Payload (Body):

{ "status": "inactive" }
const data={
  "status":"inactive"
}

const params = new URLSearchParams({
    "name": "Jade",
    "role": "user",
    "globalSearch": "trainer"
});
                    
fetch(`serve/<apiName_tokenNo>?${params.toString()}`, {
method: 'PUT',
headers: {
    'Content-Type': 'application/json',
    // Include Authorization headers if required
    'Authorization': 'Bearer YOUR_TOKEN_HERE'
},
    body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
    // Utilize the retrieved data in your application
    console.log(data);
})
.catch(error => {
    // Handle errors if the fetch request fails
    console.error('Error:', error);
});

🟢 Result: Updates all rows that match any of the given condition(s)

idnamerolestatus
1Jadeadmininactive
2Maxuserinactive
4Finnuserinactive
5Ninatrainersinactive

📝 Explanation:

  • searchMode=OR: Updates all rows where any single condition is true.

  • name=Jade: Matches rows where name is "Jade".

  • role=user: Matches rows where role is "admin".

  • globalSearch=user: Matches rows where any field includes "active".

  • Payload: The field `status` is updated to "inactive".

📌 Only one of the conditions needs to match per row for the update to apply.

🔹Update Records with AND Matching

You want to update records where any of the following is true:

  • name = Jade

  • AND

  • role = admin

  • AND

  • globalSearch = active

API URL: serve/<apiName_tokenNo>&name=Jade&role=user&globalSearch=trainer

🟡 Payload (Body):

{ "status": "inactive" }
const data={
  "status":"inactive"
}

const params = new URLSearchParams({
    "name": "Jade",
    "role": "admin",
    "globalSearch": "active",
    "searchMode": "AND"
});
                    
fetch(`serve/<apiName_tokenNo>?${params.toString()}`, {
method: 'PUT',
headers: {
    'Content-Type': 'application/json',
    // Include Authorization headers if required
    'Authorization': 'Bearer YOUR_TOKEN_HERE'
},
    body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
    // Utilize the retrieved data in your application
    console.log(data);
})
.catch(error => {
    // Handle errors if the fetch request fails
    console.error('Error:', error);
});

🟢 Result: Updates one row below that matches all given condition(s)

idnamerolestatus
1Jadeadmininactive

📝 Explanation:

  • searchMode=AND: Updates only records where all conditions are met in the same row.

  • name=Jade: Matches rows where name is "Jade".

  • role=admin: Matches rows where role is "admin".

  • globalSearch=active: Matches rows where any field includes "active".

  • Payload: The field `status` is updated to "inactive".

📌 All filters must match in a single row for the update to take effect.