Unable to POST JSON state data gotten from a GET request

I am having trouble posting data gotten from a get request to the inbox url of my project. I am using opensheet. The Github link to the project is GitHub - benborgers/opensheet: 💾 An API to get a Google Sheet as JSON, no authentication required. an open source API that converts rows of data in Google Sheets to JSON. The JSON data can be gotten from a single link as long as the spreadsheet is public. Here is the link to the public spreadsheet.

I am using the language-http adaptor with Openfn credentials only since the Google Sheets link is public. Below are my GET and POST requests.

get("<link to the opensheet link above", {

 headers: {"content-type": "application/json"},

},
function(state) {
console.log(state.data);
return state;
}
)

post("<inbox-url id", {

body: state => {
  console.log(state)
return {
  "Customer Name": dataValue('key'),
  "Phone Number": dataValue('key'),
  "Gender": dataValue('key'),
  "Date Of Birth": dataValue('key')
 
};

},
headers: {“content-type”: “application/json”},

},
function(state){
console.log(state);
return state;
}
)
Upon checking my activity history, I find that both requests succeed but the post request brings an empty object despite the GET request fetching the data. The GET request actually brings the arrays of objects from the console.log(state.data) .

Here is the console.log(state) response.

The message body also returns an empty JSON message.

Any ideas on solving this?

Hey @patrick ,

The formatting looks a little strange in your post, but I think there’s an issue with your third operation. I’ve tested the following code and it seems to do what you want:

// Get some data (no extra arguments needed... just your URL.)
get('your-spreadsheet-url');

// Inspect it (the body of the response will be in state.data)
fn(state => {
  console.log(state.data);
  return state;
});

// Do something with it (this one we need to talk about!)
post('your-inbox-url', {
  body: {
    'Customer Name': dataValue('key'),
    'Phone Number': dataValue('key'),
    Gender: dataValue('key'),
    'Date Of Birth': dataValue('key'),
  },
});

The big question is what, exactly, do you want to do with that array of customers? Right now, your third operation is building a single object, and trying to populate the values for various attributes with whatever it finds at the path: state.data.key - that’s what dataValue('key') does. If you want to send the whole payload to your inbox, your third operation should look like this:

post('your-inbox-url', { body: state => state.data })

If you want to do something with the data you get from Google Sheets, maybe map across that array and build a new array of objects with some cool new keys, you’d write something like this:

post('your-inbox-url', {
  body: state =>
    state.data.map(c => ({
      fullName: c['Customer Name'],
      howToCallThem: c['Phone Number'],
      gender: c['Gender'],
      'Date Of Birth': c['Date Of Birth'],
      whatever: true,
      otherThing: 73,
    })),
});

There, I’m mapping across that array (of customers c) and building a new array with a bunch of new keys.

Also, here are some language-http job examples:

Hi@taylordowns2000

Thank you for the quick and detailed response. This works perfect! The problem with my implementation is clearly with mapping of states with the data. I guess I need to revisit states in JavaScript and have a quick refresher. Thanks again!

@aleksa-krolls

I will be sure to check the examples and revisit the Openfn documentation once more.

1 Like

Sounds good @patrick , glad it worked!

One quick thought (cc: @santosh, @amber and our conversation the other day) is that state is just what we call the “current state of your data” at any point in the pipeline. It’s a Javascript object. Each operation:

  1. takes a Javascript object,
  2. does some things (like creating records in a DB, making an HTTP request, etc.),
  3. and then returns a Javascript object for use in the next operation.

If you wanted, you could write this and it would work just as well:

get('your-spreadsheet-url');

fn(objAfterOperation1 => {
  console.log(objAfterOp1.data);
  return objAfterOp1;
});

post('your-inbox-url', { body: objAfterOp2 => objAfterOp2.data })

Note that in the very first operation above, the “initialState” will be built for you by the platform, based on what triggered the run and what credential (if any) you’re using: