POST receiver (server)

2019-09-14 23:29发布

问题:

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.

回答1:

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:

while true; do nc -l 10000 < /dev/null ; printf '\n\n\n'; done

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':

curl --data "this-is-POST-data" http://localhost:10000

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:

while true; do printf 'HTTP/1.0 200 OK\r\nContent-type: text-plain\r\n\r\nHello, world!' | nc -l 10000  ; printf '\n\n\n'; done

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:

#!/bin/sh

printf 'Content-type: text/plain\r\n\r\n'
cat > /tmp/post-data
echo OK

call it, say, "test.sh" and place it in the CGI-Executables folder, and run:

chmod +x /Library/WebServer/CGI-Executables/test.sh

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.



回答2:

Look for SBJson framework

These are sample lines u can write to parse the GET data.

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithData:urlData];
[dictionary setDictionary:dict];
[parser release];

These are sample lines u can write to POST data.

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
jsonStr = [writer stringWithObject:dictionary];
[writer release];

There are many more methods in framework to do some useful stuffs.