Commit c8a442dd authored by Marc Egger's avatar Marc Egger

dataReport.php: add custom response header

parent 2eb7c6e1
......@@ -1692,30 +1692,33 @@ API Call QFQ Report (e.g. AJAX)
.. note::
QFQ Report functionality protected by SIP offered to simple API calls: ``typo3conf/ext/qfq/Api/dataReport.php?s=....``
General use API call to fire a specific QFQ tt-content record. Useful for e.g. AJAX calls. No Typo3 is involved.
*No FE-Group access control*.
This describes the client side (=QFQ is client). For server function check :ref:`restApi`.
Example QFQ record JS::
# Register SIP with given arguments.
10.sql = SELECT 'U:uid=12345&arg1=Hello&arg2=World|s|r:8' AS '_link|col1'
# Build JS
10.tail = <script>
function writeYourOwnAjax(){
$.ajax({
url: 'typo3conf/ext/qfq/Api/dataReport.php?s={{col1:RE}}',
data: {arg3:456, arg4:567},
method: 'POST',
dataType: 'JSON',
success: function(response) {ajaxSuccess(response);},
error: function(jqXHR, textStatus, errorThrown) {ajaxError(jqXHR, textStatus, errorThrown);}
});
}
</script>
QFQ Report functionality protected by SIP offered to simple API calls: ``typo3conf/ext/qfq/Classes/Api/dataReport.php?s=....``
* General use API call to fire a specific QFQ tt-content record. Useful for e.g. AJAX calls. No Typo3 is involved. *No FE-Group access control*.
* This defines just a simple API endpoint. For defining a rest API see: :ref:`restApi`.
* Custom response headers can be defined by setting the variable `apiResponseHeader` in the record store.
* Multiple headers should be separated by '\n' or '\r\n'. e.g.: 'Content-Type: application/json\ncustom-header: fooBar'
* If a QFQ error occurs then a http-status of 400 is returned together with a JSON encoded response of the form: `{"status":"error", "message":"..."}`
Example QFQ record JS (with tt_content.uid=12345)::
5.sql = SELECT "See console log for output"
# Register SIP with given arguments.
10.sql = SELECT 'U:uid=12345&arg1=Hello&arg2=World|s|r:8' AS '_link|col1|_hide'
# Build JS
10.tail = <script>
console.log('start api request');
$.ajax({
url: 'typo3conf/ext/qfq/Classes/Api/dataReport.php?s={{&col1:RE}}',
data: {arg3:456, arg4:567},
method: 'POST',
dataType: 'TEXT',
success: function(response, status, jqxhr) {console.log(response); console.log(jqxhr.getAllResponseHeaders());},
error: function(jqXHR, textStatus, errorThrown) {console.log(jqXHR.responseText, textStatus, errorThrown);}
});
</script>
Example QFQ record called by above AJAX::
......@@ -1723,7 +1726,7 @@ Example QFQ record called by above AJAX::
# The example above assumes that this record has the tt_content.uid=12345.
render = api
10.sql = SELECT '{{arg1:S}} {{arg2:S}} {{arg3:C}} {{arg4:C}}', NOW()
, 'Content-Type: application/json\ncustom-header: fooBar' AS _apiResponseHeader
.. _rest_client:
......
......@@ -10,7 +10,9 @@ namespace IMATHUZH\Qfq\Api;
require_once(__DIR__ . '/../../vendor/autoload.php');
use IMATHUZH\Qfq\Core\Helper\Path;
use IMATHUZH\Qfq\Core\QuickFormQuery;
use IMATHUZH\Qfq\Core\Store\Store;
/**
......@@ -36,10 +38,23 @@ $status = HTTP_400_BAD_REQUEST;
try {
try {
Path::setMainPaths(Path::API_TO_APP);
$qfq = new QuickFormQuery(['bodytext' => '']);
$data = $qfq->dataReport();
// Get custom headers from record store
$headers = Store::getInstance()->getVar(API_HEADER_VARIABLE_KEY, 'RE');
if (!empty($headers)) {
$headers = str_replace("\r\n", '$$SEP$$', $headers);
$headers = str_replace("\n", '$$SEP$$', $headers);
$headers = explode('$$SEP$$', $headers);
} else {
$headers = [];
}
$status = HTTP_200_OK;
} catch (\UserReportException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (\CodeException $e) {
......@@ -51,7 +66,15 @@ try {
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}
//header('HTTP/1.0 ' . $status);
//header("Content-Type: application/json");
//echo json_encode($answer);
// error output
if ($status === HTTP_400_BAD_REQUEST) {
header('HTTP/1.0 ' . $status);
header("Content-Type: application/json");
echo json_encode($answer);
}
// normal output
foreach ($headers as $header) {
header($header);
}
echo $data;
\ No newline at end of file
......@@ -819,6 +819,7 @@ const API_FORM_UPDATE_VALUE = 'value';
const API_FORM_UPDATE_HIDDEN = 'hidden';
const API_FORM_UPDATE_DISABLED = 'disabled';
const API_FORM_UPDATE_REQUIRED = 'required';
const API_HEADER_VARIABLE_KEY = 'apiResponseHeader';
const API_ELEMENT_UPDATE = 'element-update';
const API_ELEMENT_ATTRIBUTE = 'attr';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment