Basic Rest in atom package


#1

I am trying to make a simple post request to an API outside of Atom. I have tried using Node’s HTTP, Request, and jQuery but each of them has its own collection of errors and issues. I have yet to be able to make a successful post request. Is there anywhere that has an example of rest in a package?


Cross Package Events
How can you make ajax requests?
#2

You can use the brand new fetch API:

fetch('https://discuss.atom.io').then(function(response) {
  console.log(response);
});

See also https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch


#3

Is that using https://www.npmjs.com/package/fetch? Implementations look slightly different.


#4

no. If you follow the @olmokramer’s link, you’ll see it’s an experimental browser feature built right into Firefox, Chrome and Opera so far. One day it may become stable and a standard among browsers. As Atom/Electron is based on chrome you can make use of it without installing any additional library. :wink:

@olmokramer nice hint btw. haven’t heard of the fetch API before you mentioned it. thx!


#5

@nylki when I tried to use fetch directly I got fetch is undefined error.


#6

Atom’s still using a version of Electron (/Chromium) that doesn’t support the new fetch API - it’ll be supported once https://github.com/atom/atom/pull/7877 lands.


#7

Any idea on when it will land? I am blocked by not being able to do a post request with a data object from a package. Using the fetch npm package I’m getting odd errors when using the payload parameter so thats another dead end.


#8

I use the request npm module a lot. I’m curious what problems you had with it.


#9

Doing a post request with a data object gave me a first parameter must be a string error, post without the object went through but didn’t work with my API since it requires an configuration object.


#10

If you are using the simple form of the call then the first param is the URL which must be a string. It seems that you are having trouble with basic usage. The package is very solid.

I’m sure there are a lot of examples to be found in the repo and on the web. There is no reason to give up so easily. At worst you could ask a question on the issues list on the repo.


#11

i am following the implementation, request(url, obj, callback) when I do not put an object in with a payload then the request goes through. With the object it throws the parameter error. I can switch over again and get the exact code I implemented with.


#12

Found out what was missing: json: true,
From the documentation:

body - entity body for PATCH, POST and PUT requests. Must be a Buffer or String, unless json is true. If json is true, then body must be a JSON-serializable object.


#13

That’s exactly what I was going to tell you.

(Just kidding, I didn’t have a clue).

Once you get used to it you should like it. The docs are a bit confusing. They are great for advanced features like streaming but weak on basics.