I find myself in need of a way to test the post requests made from my application.
I'm sending my request to http://macbook-pro.local/
, and I'm trying to figure out what service I can run to read and display the request.
I looked into RestKit without any luck.
Using SignalR could work, but the port to macos isn't working as expected.
Look for SBJson framework
These are sample lines u can write to parse the GET data.
These are sample lines u can write to POST data.
There are many more methods in framework to do some useful stuffs.
What you want is basically a very simple web server, but if all you want is printing out what comes in a HTTP POST request you can get away with using the built-in 'nc' command. You can use Terminal to print out the contents of incoming requests on local port 10000 by running 'nc' in a loop like this:
You can then go to
http://localhost:10000
in your browser and see the HTTP request appear in your Terminal window. The web browser will give an error message since 'nc' isn't smart enough to reply.To test an HTTP POST request you can use 'curl':
Again, curl will give an error message because 'nc' simply closes the connection without giving a proper HTTP reply. You can have 'nc' reply with a static HTTP response to all requests like this:
If you need to use port 80 you'll need to run 'nc' as root (e.g. using 'sudo').
If you need to have any kind of real HTTP traffic going on, however, you will need to get a proper web server. OS X comes with the Apache web server which can be started with the command "apachectl start" ("apachectl stop" to stop it). CGI is enabled so you can put executables into /Library/WebServer/CGI-Executables and access them using
http://localhost/cgi-bin/filename
. For example, if you create the following CGI script:call it, say, "test.sh" and place it in the CGI-Executables folder, and run:
Then, whenever you send a POST request to
http://localhost/cgi-bin/test.sh
it will save the contents of the POST data to the file /tmp/post-data on your computer.Note: in all examples, "localhost" can be replaced with "macbook-pro.local" for accesses over the network if that is your computer hostname.
Also note that your OS X firewall permissions may block 'nc' and other software from listening to TCP ports. Usually you should get a permission dialog, but if you simply get "permission denied", tweak your firewall settings in System Preferences -> Firewall -> Firewall Options.