Process an HTTP POST Request rename
author:
contributor:
published:
updated:
source uri:
Freebase Apps Help Topic /freebase/apps/help_topic
Tag:
Status:
Summary
You can process an HTTP POST either through a template file or a script file. Use a template file...
Content
You can process an HTTP POST either through a template file or a script file. Use a template file if you want to return an HTML document (or an XML document); use a script file if you want to return JSON or JSONP.
First, decide whether your file will also support GET. This is important as often, HTTP POSTs are used to prevent cross-domain XmlHttp requests, you might want to explicitly refuse HTTP GET requests. If you're using a script file, you can use this pattern to check
>
> Only POSTs allowed.
>
>
> ... normal processing here ...
>
If you're using a script file, use this pattern
> if (acre.request.method != 'POST') {
> acre.response.status = 200;
> acre.write(JSON.stringify({ "code" : "error", "message" : "Only POSTs allowed." }));
> } else {
> // ... normal processing here ...
> }
After making that check, then respond to the request using URL parameters passed in [`acre.request.params`](http://freebaseapps.com/docs/acre_api/acre.request#member_params) and parameters passed through the POST body in [`acre.request.body_params`](http://freebaseapps.com/docs/acre_api/acre.request#member_body_params). For example,
> var query = acre.request.params["query"];
> var content = acre.request.body_params["content"];
Note that these parameters have already been URL-decoded for you. However, if a parameter is a JSON string, you have to call [`JSON.parse`](http://freebaseapps.com/docs/acre_api/JSON#member_parse) on it yourself. Or if a parameter is a boolean or a number, then you must parse it yourself (e.g., using Javascript's global functions `parseInt` or `parseFloat`).
Note that on the client-side, we recommend that you use jQuery's [`post`](http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype) method to make the POST request
>
> ...
>
> $.post(
> "script_file_id_here?query=" + query,
> { "content" : someString,
> "anotherBodyParam" : 0
> },
> function(result) {
> if (result.code == "ok") {
> // ... do something with result
> } else {
> // ... display result.message
> }
> },
> "json"
> );
Note that if you want to pass a parameter that is an array or an object, then you must serialize it yourself.
Debugging POSTs are typically harder than debugging GETs, because it's hard to simulate a POST in a browser by navigating to a URL. One way to see what your server-side code sees is to send back some internal variables in the HTTP response:
> acre.write(JSON.stringify({
> "code" : "error",
> "message" : "All hell breaks loose.",
> "body_params" : acre.request.body_params
> }));
And use something like Firebug to inspect the HTTP POST request and response.
First, decide whether your file will also support GET. This is important as often, HTTP POSTs are used to prevent cross-domain XmlHttp requests, you might want to explicitly refuse HTTP GET requests. If you're using a script file, you can use this pattern to check
>
> Only POSTs allowed.
>
>
> ... normal processing here ...
>
If you're using a script file, use this pattern
> if (acre.request.method != 'POST') {
> acre.response.status = 200;
> acre.write(JSON.stringify({ "code" : "error", "message" : "Only POSTs allowed." }));
> } else {
> // ... normal processing here ...
> }
After making that check, then respond to the request using URL parameters passed in [`acre.request.params`](http://freebaseapps.com/docs/acre_api/acre.request#member_params) and parameters passed through the POST body in [`acre.request.body_params`](http://freebaseapps.com/docs/acre_api/acre.request#member_body_params). For example,
> var query = acre.request.params["query"];
> var content = acre.request.body_params["content"];
Note that these parameters have already been URL-decoded for you. However, if a parameter is a JSON string, you have to call [`JSON.parse`](http://freebaseapps.com/docs/acre_api/JSON#member_parse) on it yourself. Or if a parameter is a boolean or a number, then you must parse it yourself (e.g., using Javascript's global functions `parseInt` or `parseFloat`).
Note that on the client-side, we recommend that you use jQuery's [`post`](http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype) method to make the POST request
>
> ...
>
> $.post(
> "script_file_id_here?query=" + query,
> { "content" : someString,
> "anotherBodyParam" : 0
> },
> function(result) {
> if (result.code == "ok") {
> // ... do something with result
> } else {
> // ... display result.message
> }
> },
> "json"
> );
Note that if you want to pass a parameter that is an array or an object, then you must serialize it yourself.
Debugging POSTs are typically harder than debugging GETs, because it's hard to simulate a POST in a browser by navigating to a URL. One way to see what your server-side code sees is to send back some internal variables in the HTTP response:
> acre.write(JSON.stringify({
> "code" : "error",
> "message" : "All hell breaks loose.",
> "body_params" : acre.request.body_params
> }));
And use something like Firebug to inspect the HTTP POST request and response.
Recent Discussions about Process an HTTP POST Request
There is no discussion about this document.
Start the Discussion »