I try to use kotlin-js to get data from external w...
# javascript
m
I try to use kotlin-js to get data from external webservice. I try with this code :
Copy code
val request = XMLHttpRequest()
request.open("GET", endpoint, false)
request.send(null)
if (request.status.toInt() == 200) {
  return JSON.parse(request.responseText)
}
And I get this error :
XMLHttpRequest is not defined
. My code run on NodeJS and NodeJS doesn't have XMLHttpRequest... Should I use another lib and wrap it ?
I try to add https://www.npmjs.com/package/xmlhttprequest to my project (yarn add) wihout success...
b
probably the best way to work with http on Node.js is https://nodejs.org/api/http.html
t
The easiest library to use under NodeJS is https://www.npmjs.com/package/request. You can combine it with
request-promise-native
and
kotlinx.coroutines
if you want to. If you want to stay close to the standard you can use https://www.npmjs.com/package/node-fetch and you will get something similar to what you can have in your browser. Or you can just go with the API which has been mentioned before.
m
Should I wrap these libs ? It's not a part of KotlinJS ?
t
KotlinJS provide wrappers only for Web API and DOM AFAIK, so you have to do it 😕 Or you can build a library that support multi-platforms 😛 There is a good library named Fuel, but they need help to make it working with JavaScript. If you have some time https://github.com/kittinunf/Fuel/issues/134