Home > database >  How to use Query Curl on Nodejs
How to use Query Curl on Nodejs

Time:12-23

I tried that on Replit: Picture

but gives that error(On Picture)

I tried that: https://docs.dlive.tv/api/api/query

Why Didn't work?

Edit --> I fixed! code: line 11: JSON.stringify({"query":"query{userByDisplayName(displayname: "POTATO") {username displayname avatar partnerStatus followers{totalCount}}}"})

CodePudding user response:

You are sending formdata while JSON was expected. Send JSON instead: Use JSON.stringify, not querystring.stringify.

But beyond that, you also have the problem that you are sending a GraphQL query which is, astonishingly, also valid JavaScript (syntax-wise at least) but will not turn into the thing you want once sent. Enclose the GraphQL code in backticks ` (and remove the :s and change the = to :).

However, using node-libcurl for this, let alone in low-level mode, seems awfully and unnecessarily complex for this task. I suggest using something simple like axios instead:

const axios = require('axios')

async function main () {
  try {
    const result = await axios.post('https://graphigo.prd.dlive.tv/', {
      query: `
        query {
          userByDisplayName(displayname: "POTATO") {
            username
            displayname
            avatar
            partnerStatus
            followers {
              totalCount
            }
          }
        }    
      `
    })
    
    console.log('Result:', result.data)
  } catch (e) {
    if (e.response) {
      console.error('An error occured! Website returned:', e.response.data)
    } else {
      throw e
    }
  }
}

// This exists just so that we can use the async function
// without unhandled rejections.
main().catch(e => { console.error(e); process.exit(1) })

See replit: https://replit.com/@CherryDT/AxiosTest#index.js

  • Related