Error calling Influx API Service

i try to replicate this code in a simple nodejs code

curl -XPOST "http://localhost:9999/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
  --header "Authorization: Token YOURAUTHTOKEN" \
  --data-raw "mem,host=host1 used_percent=23.43234543 1556896326"
const axios = require('axios')
const qs = require('qs')

async function influxd(){

    const options = {
        
      url: 'http://ec2-public-ip:9999/api/v2/write?org=something&bucket=something&precisions=s',
      method: 'POST',
      headers: {
        'Authorization': 'Token 1LNrO7r1NPWDAwYtZpl-0Zd1EjSSwPhimoKZ8PUcTSbFFhqCIUUjYVYIA6ReFfqYUMuqpIPF1x33-pCcsp99fA==',
        'Content-Type': "text/plain; charset=utf-8"
      },
      timeout: 0,
      'body': qs.stringify('cpu,host=host1 usage_user=3.8234,usage_system=4.23874 1556892726597397000')
    }
  
    const response = await axios(options).catch(error=>console.log(error))

    return response
  
}

and i receive this error

 data: { code: 'invalid', message: 'writing requires points' } },

what i’m doing wrong?

A few issues:

  • I think the 'body' parameter should be data, and you can just pass it a string. Not sure why you need qs here.
  • Make sure that your Org uses the orgID and not the org name.
  • The timestamp in the line protocol you’re submitting uses nanosecond precision, so remove the precisions=s from the URL.

The following code worked for me:

const axios = require('axios')

async function influxd(){

    const options = {
        
      url: 'http://localhost:9999/api/v2/write?org=org&bucket=bucket',
      method: 'POST',
      headers: {
        'Authorization': 'Token token',
        'Content-Type': "text/plain; charset=utf-8"
      },
      timeout: 0,
      data: 'cpu,host=host1 usage_user=3.8234,usage_system=4.23874 1574786860084000000'
    }
  
    const response = await axios(options).catch(error=>console.log(error))

    return response
  
}