Home > database >  Javascript API POST from JSON file with fs readFile and map
Javascript API POST from JSON file with fs readFile and map

Time:01-13

Here with javascript we do an API POST using a external JSON file. Then we have 2 files order.json and app.js

my server: ubuntu 22.04 node v19.2.0 npm 8.19.3

The script get data from the file order.js using readFile. app.js

var myHeaders = new Headers();
myHeaders.append("api-key", "123");
myHeaders.append("Content-Type", "text/plain");
myHeaders.append("Cookie", "session_id=123");

// get order.json file
var fs = require('fs')
fs.readFile('order.json', 'utf8', (err, data) => {
    if (err) throw err
    let orders = JSON.parse(data)

    let order_line = orders.map(o => ([0, 0, {
        product_id:  o.product_id,
        product_uom_qty:  o.product_uom_qty,
        price_unit:  o.price_unit
    }]));

    let body = {
        partner_id: 150,
        user_id: 6,
        workflow_id: 1,
        order_line
    }

    let raw = JSON.stringify(body);

    console.log(raw);



    var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
    }


    fetch("https://mysite/api/sale.order/create?api_key=123", requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error))

})

the file is located on ./ and contain the common order sales from your store order.json

[
    {
        "sku": "123",
        "product_id": "6",
        "price_unit": "9.7",
        "product_uom_qty": "1.0000"
    },
    {
        "sku": "456",
        "product_id": "7",
        "price_unit": "9.8",
        "product_uom_qty": "1.0000"
    }
]

CodePudding user response:

Don't create JSON yourself, but use an existing well working serialization method (like the builtin JSON.stringify)

let orders = [
  {
    sku: 123,
    product_id: 6,
    price_unit: 9.7,
    product_uom_qty: 1.0,
  },
  {
    sku: 456,
    product_id: 7,
    price_unit: 9.8,
    product_uom_qty: 1.0,
  },
];

let order_line = orders.map(o => ([0,0, { 
  product_id: o.product_id,
  product_uom_qty: o.product_uom_qty,
  price_unit: o.price_unit
  }]));

let body = {
  partner_id: 150,
  user_id: 6,
  workflow_id: 1,
  order_line 
}

let raw = JSON.stringify(body);

console.log(raw);

Mind that I initialized orders from an array instead of reading it from the disk, but that doesn't really matter, assuming that orders.json contains valid JSON data. But I noticed, the datatypes in your orders.json differs from the datatypes in the data posted through postman. Ie for instance price_unit is a string in your JSON, but a number in your posted data. You will need to fix that. I fixed this in the orders array in my snippet.

If you can't influence the structure of your json, you can also convert between string and number while creating your object like this (notice the in front of the property access, which converts the string from your order object into a number)

let order_line = orders.map(o => ([0,0, { 
  product_id:  o.product_id,
  product_uom_qty:  o.product_uom_qty,
  price_unit:  o.price_unit
}]));

I also used Array.map instead of the for loop ...

This will create a valid JSON string that can be correctly parsed at the server.

  • Related