Receive simple POST request, using HTTP package?

So I’m kind of confused on the terminology here. I am working with the HTTP package (Meteor version is 1.9 so fetch isn’t available), and I am hoping to have it receive POST data from another site sending it.
That is, the site (A) opens my meteor app (B) and sends it data via the POST method in a form. Basically, If B was PHP instead of Meteor, I would just use $_POST.

HTTP package says it provides the API to make HTTP requests
Does this mean that it can be used as mentioned above (A sends POST data to B) or does that mean it can only make it so B can send to A (or any other site)?

If the latter is true is there a good package for doing what I intend to do, that is, receive POST data on my app? One that works with Meteor v1.9, I don’t intend to update the project version for this project in particular.

Thank you!

look here webapp | Meteor API Docs

2 Likes

I’m using webapp for this and it’s really snappy and good. I think it’s understated a bit, very powerful part of meteor. I built an entire API through it in a day. It’s really awesome

So in that case, if I’m reading this right, to simply look at the information sent via POST data (where name=value)
Using Meteor’s webapp package, in connectHandlers.use((req, res, next)) I would use the req object’s data, correct? As it says “this argument can be used to get information about the incoming request.”

Yes you’d use req.body

Alternatively, for GET vars you can use req.originalUrl this gives the URL you can then take GET vars from there (this may be easier if its just a ID or something, it’s a one or two liner with a regex to match)

So let’s say I do this on the server main.js:

var bod;
WebApp.connectHandlers((req, res, next) => {
var bod = req.body();
});

Is that how it would be done? Am I missing something?

var bod;

If I was you, I won’t you global variable.
The WebApp should look like this:

WebApp.connectHandlers.use("/api", async (req, res, next) => {
  console.log("request headers", req.headers);
  if (req.method !== "POST") {
    // in this example I'm checking if the method is POST
    res.end();
    return;
  }
  // do something here
  res.writeHead(200); // server responses with 200 status code
  res.end(
    "Some data response here"
  );
});

I need the global variable because I need to use it as part of a query used to get data that is displayed in a dropdown. That code is on the client side.

I’m assuming I have to write the above code on the server main.js? And then get the variable somehow with the client main.js? Or can I just do it on the client main.js?

Also is res.writeHead(200) and res.end() required in the WebApp code? I tried my code and it crashed because of an error in a promise in WebApp’s API code.

That code runs on server. On the client side you can’t receive requests.

Okay so one final question (I believe):

Let’s say my link to the app (where the POST is sent from) is

http://abc.com/this part varies/

And let’s say my app is located at

app.com

In the [path] parameter, .use("[path]"…
should that be “abc.com”? or “app.com”?

Thanks for the help!

your meteor app run at domain app.com then you use webapp package to listen/response the POST request on this domain: app.com

I’m talking about the [path] parameter in WebApp.connectHandlers.use([path], handler)