Storing API Post Response

I am currenlty working on a job using Lightining V2.10.15 where I am creating multiple Post requests to Odoo. I can successfully post to the Odoo instance but i would like to store the response from the each post reqeust. what is the best way to go about this?

here is the sample of the code which is used to send the post request to Odoo.

each(
β€œ$.newProducts[*]”,
post(state => {

const { sku, quantity, location_id, product_id } = state.data;
const company_id = "1";

if (!product_id) {
  console.log(`Skipping stock update for new SKU: ${sku} (missing product_id)`);
  return state;
}

const url2 = `create?model=stock.quant&values=%7B%20%20%20%22product_id%22%3A%20${product_id}%2C%20%20%20%22location_id%22%3A%20${location_id}%2C%20%20%20%22quantity%22%3A%20${quantity}%2C%20%20%20%22company_id%22%3A%20${company_id}%20%7D`;

console.log(`Updating stock for new SKU: ${sku} - URL: ${url2}`);

return url2 ;

})
);

1 Like

Hey @Kiefer!

Couple of things here!

First off, when using http, the next state object should include a response.

To get hold of it, you can use a .then() call back, like this:

post(url).then(state => {
   const { response, data } = state;
   state.allResponses.push(response)
  return state
});

And you can nest that in your each:

each(β€œ$.newProducts[*]”,
  post(url).then(state => {
     const { response, data } = state;
     state.allResponses.push(response)
    return state
  });
)

Secondly, we have an odoo adaptor now! odoo@1.0.1 | OpenFn/docs

It doesn’t expose the response to you, but it does make the creation of records a bit easier. I’m not sure if it’s suitable for you but maybe worth a look?

2 Likes