Clarification required on POST and ability to access server side objects


#1

Hello,

I’m having some issues in being able to access the server side objects following a POST - something similar to what was discussed here (https://github.com/iron-meteor/iron-router/issues/452).

I’m not sure if this has been resolved (most likely) or there’s a work around…?? I couldn’t see anything in the example code that I don’t already have included.

Other articles refer to the adding of the following lines of code (assuming I’ve understood this correctly!):

this.response.statusCode = 200;
this.response.setHeader(“Content-Type”, “application/json”);
this.response.setHeader(“Access-Control-Allow-Origin”, “*”);
this.response.setHeader(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, Content-Type, Accept”);

Unfortunately, I’m finding that the request.query and request.params objects are empty.

Any ideas as to what I’m doing wrong…?

Meteor v1.0.3.2
iron:router v1.0.7

Thanks

Nutz


#2

Make sure the request coming in has the correct headers set (Content-Type). I noticed that when sending POST data via

curl -s -X POST -d token=${TOKEN} ${URLENDPOINT}

I am able to access the parameters (token) like

Router.route('/api/endpoint', {where: 'server'})
.post(function(){
 // get token from body and save it in user record
 var req = this.request;
 var res = this.response;
 var json = {success: false, userId: null};
 if('token' in req.body)
 {
  json.success = true;
  // .... more code
 }
 json = EJSON.stringify( json );
 this.response.writeHead(200, {
  'Content-Length': json.length,
  'Content-Type': 'application/json'
 });
 this.response.end(json);
})

#3

Hi,

Thanks for the response. Could you explain a little further for me?

The sketch that runs on my Arduino is responsible for the POST to the Meteor server. I have this publishing ‘application/x-www-form-urlencoded’ as a content type. I did change this to ‘application/json’ (and the associated data payload format) but I’m still seeing empty request objects.

Issuing: curl -v ‘http://192.168.1.178:3000/sensordata?sensortype=222’ results in a GET method call and this works - I can see the request object and params populated.

Issuing: curl -v ‘http://192.168.1.178:3000/sensordata’ -d “sensortype=222” results in empty request objects.

Thank you,

Nutz


#4

Don’t you have to provide -X POST to the curl call?


#5

Possibly…but it appears to work without. The key thing here is that my sketch is not using “curl” to generate the POST…so still stumped as to what I need to do here…

[MyApp2_logfile2] Method:POST
[MyApp2_logfile2] Params data (sensortype):undefined
[MyApp2_logfile2] Params data:{}
[MyApp2_logfile2] Received data:{}
[MyApp2_logfile2] Received data (sensortype):undefined
[MyApp2_logfile2] Received data (sensorname):undefined
[MyApp2_logfile2] Received data (sensorvalue):undefined
[MyApp2_logfile2] Method:GET
[MyApp2_logfile2] Params data (sensortype):“222”
[MyApp2_logfile2] Params data:{“sensortype”:“222”}
[MyApp2_logfile2] Received data:{“sensortype”:“222”}
I[MyApp2_logfile2] Received data (sensortype):222


#6

I have the following in my test case. Here is my template, where the {{random}} is just the actual time

<template name="hello">
  <h1>Welcome to Meteor!</h1>

  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
  <form name="myform" id="myform" method="post" action="/webhooks/stripe/{{random}}">
  <textarea name="text"></textarea>
  <input name="submit" type="submit">
  </form>

  <pre style="white-space: pre-line"> {{json}}</pre>
</template>

and the event handler

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set("counter", Session.get("counter") + 1);
    },
    'click input[type="submit"]': function(e,template) {
      e.preventDefault();
      var form=template.$('#myform');
      $.post( form.attr("action"), form.serialize(), function(res){
          Session.set('json',res);
      });
    }
  });

which send it to

Router.route('/webhooks/stripe/:id', { where: 'server' })
  .post(function () {
    // POST /webhooks/stripe
    console.log(this.request.query);
    var obj = this.request.body;
    obj['id'] = this.params.id;
    var json = EJSON.stringify(obj, {indent: true});
    this.response.writeHead(200, {
      'Content-Length': json.length,
      'Content-Type': 'application/json'
    });
    this.response.end(json);
  })


#7

Thank you. I’ll review.


#8

I’m unable to get this to work if the sketch running on the Arduino is making a POST to the Meteor server. I have however been able to make this work if I use GET when calling the server and this works as expected.

Thanks for your assistance.

Nutz


#9

What exactly is the sketch sending out (headers and all)?


#10

Have you looked into handling the request on the server without Iron Router? Check out webapp docs. If I am not mistaken they allow raw access


#11

Hi, no I’ve not looked at not using Iron Router…I’ll take a look at the link. I do have this working now if I use GET… This is the snippet of code that the Arduino is running. The difference between this and the previous attempt was that I was using POST and passing the data in JSON format (so using different content-type than the one below).

rawSensorData = "sensorid=" + rawSender + "&sensortype=" + rawSensor + "&messagetype=" + rawMessageType + "&messagesubtype=" + rawMessageSubType + "&sensorvalue=" + rawPayload;

if (meteorServer.connect(Meteor_Server_Address, 3000))
{
	if (meteorServer.connected())
	{
		meteorServer.println("GET /sensordata?" + rawSensorData + " HTTP/1.1");
		meteorServer.println(F("Host: 192.168.1.178"));
		meteorServer.println(F("Accept: application/x-www-form-urlencoded"));
		meteorServer.println(F("Content-Type: application/x-www-form-urlencoded"));
		meteorServer.print(F("Content-Length: "));
		meteorServer.println(rawSensorData.length());
		meteorServer.println(F("Connection: close"));
		meteorServer.println();
		meteorServer.print(rawSensorData);
		delay(500);
		meteorServer.stop();
	}
	else
	{
		Serial.println("Not connected...");
	}
}