How to determine HTTP method?

Discussion about using Moai Cloud - post questions, bugs and issues here.

How to determine HTTP method?

Postby joarb » Mon Aug 06, 2012 11:44 am

I'm having trouble differentiating between GET and POST requests to the cloud. Here's my clouds main method:
Code: Select all
  1. function main( web, req )

  2.   local mehtod = web:method ( )

  3.   if ( method == GET ) then

  4.     web:page ( "Get.", 200, "OK" )

  5.   elseif ( method == POST ) then

  6.     web:page ( "Post.", 200, "OK" )

  7.   end

  8. end

However, I do not know the proper named constants (if any) to be used in place of GET and POST. How can I get the conditional to catch a POST request? Thanks! :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: How to determine HTTP method?

Postby rob » Mon Aug 06, 2012 12:31 pm

You should be able to use the string "GET" or "POST". The above code is probably comparing the method to nil since GET or POST haven't been defined as constants.
rob
 
Posts: 116
Joined: Wed Sep 21, 2011 2:34 pm

Re: How to determine HTTP method?

Postby joarb » Mon Aug 06, 2012 1:20 pm

Ahhh, so close. I did try to return the value of web:method ( ) to the caller (using web:page ( ) ), but it crashed. I guess there was another problem with the script as well causing the crash. That's why I concluded that web:method ( ) wasn't a string. Anyways, thanks again Rob!
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: How to determine HTTP method?

Postby joarb » Tue Aug 07, 2012 7:00 am

I guess I was a little too quick in my last post. Tried to return method to caller like so:
Code: Select all
  1. function main( web, req )

  2.   local mehtod = web:method ( )

  3.   web:page ( method, 200, "OK" )

  4. end

but all I get is
Error

bad argument #1 to 'length' (string expected, got nil)
Stack Trace

1 C function
2[(tail call)]:-1
3[[string "main.lua"]]:130
4[(tail call)]:-1
5 C function
6 C function
7 C function
Apparently, somethings wrong with the above code. Do anyone know how to get this to work?
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm

Re: How to determine HTTP method?

Postby rob » Tue Aug 07, 2012 7:19 am

That is a bug in the tir framework that happens if there is an empty or nil body. It's something I've been meaning to patch for a while.

In the code above you have a typo, ou assign the value to 'mehtod' but you are sending 'method' to web:page. Just yesterday we added a 'Strict Mode' to the cloud that would help to catch typo errors like these. You can turn it on in the edit service page.
rob
 
Posts: 116
Joined: Wed Sep 21, 2011 2:34 pm

Re: How to determine HTTP method?

Postby joarb » Tue Aug 07, 2012 7:27 am

Wow - sorry about bugging you with typos (I've set my application to 'Strict Mode' now). Thanks! :)
joarb
 
Posts: 125
Joined: Thu Jan 19, 2012 12:47 pm


Return to Moai Cloud

Who is online

Users browsing this forum: No registered users and 0 guests

x