API Post Request


#1

Hello,

I have some data that i need to POST to the webserver. However I am completely how to do that. Basically I need to send data on button click. What have I tried so far:

  1. Simple post action from the method
  2. Tried to mix with Angular
  3. Tried npm packages request and superagent (couldn’t make them work on click)

Looking for some kind of advice, I am stuck here for a whole day. Thanks :).


#2

If by #1 you’re referring to the method in the Node HTTP module for making requests, then that should be the way to do it. Have you had somebody look at your code to see what’s going wrong?


#3

Parod me if I am being an newbie. If I am right the npm request happens on the backend, however I need to make a mixin with frontend, so the request happens only on button click. Request itself works fine when it is not mixed with button “onclick”.


#4

I think you think that there’s more of a distinction between frontend and backend in JavaScript than there actually is. The “front” is just everything the user sees, and the “back” is just everything they don’t. All you need is to have a button on some page in your app call a function in a file that has a require("http") statement.

NPM is the package manager for packages in the Node.js ecosystem. If you say “the npm request”, people are going to go looking here to find out what you’re talking about. If you actually mean a built-in Node module, don’t say it’s from NPM.


#5

The more modern way to perform async requests to an API server is via fetch. This is quite new, but it is supported in the node version current Atom is built on, and it is to be preferred.

There are plenty of examples online, but if you want to see an example of it working within Atom, then you can look at the source code of geiger, a package that I maintain myself. More specifically, you should look here.

HTH!


#6

Thanks for your great answer :).