1

I have this d3 script that generates a table based on the returned JSON results of a post request to a dynamic API.

d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search")
.header("Content-Type", "application/json")
.post("intent=data-quality", function (error,data){

                      function tabulate(data, columns) {
                            var table = d3.select('#response').append('table')
                            var thead = table.append('thead')
                            var tbody = table.append('tbody');

                            // append the header row
                            thead.append('tr')
                              .selectAll('th')
                              .data(columns).enter()
                              .append('th')
                                .text(function (column) { return column; });

                            // create a row for each object in the data
                            var rows = tbody.selectAll('tr')
                              .data(data)
                              .enter()
                              .append('tr');

                    console.log(data)
                      // create a cell in each row for each column
                      var cells = rows.selectAll('td')
                              .data(function (row) {
                                return columns.map(function (column) {
                                  return {column: column, value: row[column]};
                                });
                              })
                              .enter()
                              .append('td')
                                .text(function (d) { return d.value; });
                      return table;
                    }
                        // render the table
                        tabulate(data, d3.keys(data[0]));  

                    });

When executing this post request to the API this is how it is expected to work passed input parameter: example request body example request/response

However when I'm running this script, I get a 400 error code returned. I think I know what I am missing which might be passing the request body? Not 100% certain though. Any help is appreciated.

Daniel Tesfaye
  • 81
  • 1
  • 10

2 Answers2

4

Looking at your curl, my guess would be:

var data = {
  "action": "string",
  "fields": [
    "string"
  ],
  "filters": {}
};

d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search?intent=data-quality")
  .header("Content-Type", "application/json")
  .post(JSON.stringify(data), function (error,data) {

    ...
Mark
  • 101,574
  • 16
  • 158
  • 219
0

Try

d3.request("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search")
  .header("Content-Type", "application/x-www-form-urlencoded")
  .mimeType("application/json")
  .post("intent=data-quality", function (error,data){...});

And if you are expecting JSON response, you can just use d3.json instead.

Duk
  • 125
  • 1
  • 12