Skip to content
Advertisement

Send full response back along with modified key-value pairs to other API after getting data from one API

I’m getting this as the response from an internal API of my company:

Sample:

{
"name":"kolich",
"platform":"web",
"usertype":"paid",
"age":22,
"gender":"male",
"activityStatus":"active",
"isNewUser":false

}

I’m modifying the values of these keys and just sending them to the update api using post request

payload:

{
"activityStatus":"inactive",
"isNewUser":true

}

Problem :

When I am sending only those updated values to the API, all other values of other keys except name are becoming empty. How can I send updated key values along with the non updated key value pairs?

I’m using Google sheets and apps script do the above operations.

How can I send full response back to the API along with modified key value pairs? Sample only has 7 fields but I actually have 31 fields. How can I achieve it?

P.S – I cannot make any changes to the API code.

Advertisement

Answer

This is fairly straightforward with the Object Spread Syntax. You should just be able to send back:

{...response, activityStatus: "inactive", isNewUser:true}

But as Sergey points out, you could also mutate the object you get, if you have no further use for it or if you need to use it with those changes applied:

const process = async (...args) => {
  // do some work
  const response = await getTheData()
  // do some more work

  response.activityStatus = 'inactive';
  response.isNewUser = true

  // do even more work
  const acknowledgement = await updateTheServer(response)
  // you get the idea 
}

While I personally prefer to avoid mutating input objects, the choice is yours.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement