Using HTTP request to send URL parameters to Meteor app

All right I have asked similar question on here but was never answered but this one will be just about this and very specific.We are using an Arduino Uno with the Sim5281 Sheild. Here on that link are example HTTP code to send URL parameter values to a web server using a GET request, but I will be using a POST request. What I want to know is how to accept these parameters and insert them into my collection.

My collection has 9 different params including a name, latitude, and longitude. All I want the website to do is insert the parameter values. I have seen this response by geoffrey but still do not know if this will work for sure. stack-overflow.

I believe this package I wrote is the easiest way to do that:

https://atmospherejs.com/simple/json-routes

1 Like

I do have a few questions about how your package works especially where does this code go? Is it on the client or server side or in the middle.

Also how would this example be changed with the request I will be using.

`JsonRoutes.add("get", "/posts/:id", function (req, res, next) {
    var id = req.params.id;
    
    JsonRoutes.sendResult(res, {
         data: Posts.findOne(id)
    });
});`

I assume the Posts.find will just be changed to my insert but what about the top of the code. would I change it to

`JsonRoutes.add("POST"`

Although after that I don’t know were to go with the

 `"posts/:id", function(req,res,next)`

Since my url / will be /insert this is just a page and as you can see below will be the POST request. However what is the function part. Is this generic and always used? I am very lost when it comes to how to use this package. And the example of using the Iron-Router in the link that I provided seems a lot easier. I just need to know if that will work. I have never worked with JSON and am very unsure on any of its functions.

`POST /insert?temp=1&pressure=2 HTTP/1.1\r\nHost: URL\r\nContent-Length: 0\r\n\r\n`

For a very low level web programmer the package is very hard to understand how to use. I will look online to see if I can see some examples of it in use to see exactly what is going on.

It goes on the server.

JsonRoutes exposes the express/connect request and response objects, so if you’re not sure how to do something there, you should google how to do it with express. Chances are that it will work. In your case, the parameters you want are on the req.query object, so you can do something like that:

MyCollection.insert({ temp: req.query.temp, pressure: req.query.pressure });

Of course you should do some sort of validation or parsing on the data before you insert it. For example parsing temp and pressure to float, otherwise they will get stored as strings.

@helfer thank you very much. This is very helpful. Although I already knew that would be the format for inserting this still does not answer my questions.

I just want to use this on the home page to catch any POST request being sent. I will obviously format the data before use. I guess the code would look something like this?

`JsonRoutes.add("POST", "/",function(req,res,next){
    var temp = req.query.temp;
    var pressure = req.query.pressure;
    //... etc

     //Format variables here--    

    JsonRoutes.sendResult(res,{
        data: Colleciton.insert({".....formatted params. ...});
     });
 });`

I am not sure if I should use res, req, or next here. Would this be correct?

@sashko Also tried running a simple print to console using this and received the error [Uncaught ReferenceError: JsonRoutes is not defined] I have added the package to the app.

Did you run meteor add simple:json-routes?

Also, from your example it looks like you’re misunderstanding what sendResult does. Send result sends back a response. In your case, it seems that all it should send back is “OK”, if anything. Here’s what you should do:

  1. Extract the variables from the request
  2. Insert into collection
  3. sendResult
  4. Call next(); (I’m not sure if this is necessary, but it won’t hurt)

So inserting into the collection should take place right after where you wrote //Format variables here--, and then send back the response afterwards.

Ah I see. Ya I think there was a misunderstanding and yes I did run the add command. I looked online and have seen other people with the same problem.

I also tried the code suggested in the stackoverflow link and it works. But if I need a response I will look into this simple:json-routes.

Are you maybe trying to call JsonRoutes on the client?? Your code that uses JsonRoutes should be in the server directory, or wrapped with if (Meteor.isServer){ }

Ah that must have been the issue. there is no error now. I had it in the lib file. Although I will be sending a URL parameter as such

 `POST /insert?a=1&b=2 HTTP/1.1\r\nHost: MY-URL\r\nContent-Length: 0\r\n\r\n`

were a and b would be my variables. We don’t really need to send anything back. I have gotten this to work currently without using that package but if I need to send a response I will go to that package but I still don’t now how to format the first line.

What I did

In router.js in the lib file

`Router.route('/insert',{
     where: 'server',
     action: function(){
         var name = this.params.query.name;
         console.log(name);
     }
 });`

This works perfect when tested by going into the browser and typing in what I want with

 `My-URL/insert?name=Variable_Name`

POST requests should send data via the post body, not as URL parameters, btw.

That is kinda what has me very confused because the example code given in the sheild tutorial shows that code above as the way to send a Get request. I just changed the Get to a Post. I send a inquiry to the developer forums for the device and they simply rote me off saying I should figure it out.

I am currently looking into getting some type of response of help from them. I thank you guys for the option of this package and I do see it as a very useful package I just need to figure out what is going on with the Arduino code now. Which is not part of this forum.

But again I do thank you for the help. I can get a little crass when someone responds with just use this package.