I need to return this:
return header('Content-Type: image/png');
echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=');
by using FOSRestBundle but I don't know how to handle the echo in there. I have this code:
public function getTrackingEmailAction(ParamFetcher $paramFetcher, Request $request)
{
$view = View::create();
$response = [];
$view->setData($response)->setHeader('Content-Type', 'image/png')->setStatusCode(200);
return $view;
}
How do I send the base64 for display the 1x1 pixel? Do I need a view? (twig view)
According to the upper code snippet, you don’t have to return Base64. Instead, the opposite is true: you have to return the raw image data, which is merely Base64-encoded as having binary stuff inside PHP code is not so nice ;-)
In your controller action, you therefore only have to return a proper Response with the image data as content
return new Response(base64_decode('...'), 200, ['Content-Type' => 'image/png']);
Related
I am using the below sample code to return data to the browser
$response->write($image);
return $response->withHeader('Content-Type', 'image/jpeg');
which works fine but how would i go about it if i wanted to also return Content-Length ?
the only option i found so far is to copy the response into a new object like below but i don't think that's efficient if i have a $image in the response.
$response = $response->withAddedHeader('Content-Length', strlen($image));
I tried it as array but that doesn't work..
Quoting from Slim 3 docs
Reminder
Unlike the withHeader() method, this method appends the new value to the set of values that already exist for the same header name. The Response object is immutable. This method returns a copy of the Response object that has the appended header value.
Both withHeader() and with appendedHeader() methods return a copy of the response object. So even if your do not assign the return value of $response->withHeader() to a variable and return the result directly, you are still working with a copy of the response object.
Regarding your concern about efficiency, you should use streams instead of string as response body. The following is an example of how to use streams for returning an image as the response:
<?php
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Body;
return function (App $app) {
$container = $app->getContainer();
$app->get('/', function(Request $request, Response $response) {
$image = fopen('sample.jpg', 'r');
return $response->withBody(new Body($image))
->withHeader('Content-Type', 'image/jpeg')
->withAddedHeader('Content-Length', fstat($image)['size']);
});
};
I have the following route:
Route::get('echo',function (Request $req) {
return response()->stream(function () use ($req) {
echo json_encode($req->all());
}, 200, [
'Content-Type' => 'application/json'
]);
})->name('echo');
For the sake of simplicity lets assume it's a simple echo response. In reality it's a very large file. The outcome in either case is the same.
Now I want to test this route to see whether I indeed can see that json content so I've tried this:
public function testBasicTest()
{
$response = $this->get(route('echo', [
"content"=>"some content"
]));
$response->assertSeeText("some content"); //Doesn't work
$response->assertJson( [
"content"=>"some content"
]); //Neither does this
}
I've inspected it a bit further and it appears to be because (a) the response is wrapped around a TestResponse (b) the response content is never streamed and (c) even if the response content were to be forcibly streamed via $response->baseResponse->sendContent() the actual content is echoed and not actually captured by the TestResponse
In addition calling $response->getContent() does not work because it seems to directly call the StreamedResponse::getContent() which is hard-coded to return false.
I have managed to have some limited success using:
ob_start();
$response->sendContent();
$result = ob_get_clean();
however this looks like a very sloppy thing to do in a unit test.
Has anyone else encountered this before? Is it possible to test the contents of a streamed response?
This is not a good solution, more of a hack, but if anyone else encounters this issue here's what you can do:
public function testBasicTest()
{
$response = $this->get(route('echo', [
"content"=>"some content"
]));
if ($response->baseResponse instanceof StreamedResponse) {
ob_start();
$response->sendContent();
$content = ob_get_clean();
$response = new TestResponse(
new Response($content,
$response->baseResponse->getStatusCode(),
$response->baseResponse->headers->all()
)
);
}
$response->assertSee("some content"); //Works
}
A bit late to the party but it may help someone else.
In Laravel you can do $response->streamedContent() when handling a StreamedResponse (since 5.8 I believe).
Even tho my debugger told me the streamed content of my response was null I still got the data out of it.
In my case it was CSV so in my functional tests I've done :
$res = $this->post('api/v1/entity/export', $payload, $header);
$res->assertStatus(200);
$res->assertHeader('Content-Disposition', 'attachment; filename=entity.csv');
$reader = Reader::createFromString($res->streamedContent());
// tests...
I used LeagueCSV (bc it was CSV, obviously) but I'm sure you can do the same with Json or other.
Laravel doc for TestResponse
I'd like to generate a JavaScript file for JSONP data exchange.
All fine, but I need / want to set the header as :
header("Content-Type: text/javascript");
or
header("Content-Type: application/javascript");
Is this possible in a response from a controller in Laravel 4, or do I need to create a view and set the header with PHP?
I'd like to output something like:
var obj = JSON.parse('{"item1":"value1","item2":"value2"}');
// then do whatever with the object
$('#somediv').html(obj.item1);
Thanks for your help in advance
Okay, looks like I have to answer my question myself :-). Thanks to #terrylow for trying though.
Here is the way to change the header of my response using my function in my controller
public function javascriptResponse(){
$statusCode = 200;
$content = "var obj = JSON.parse('{\"item1\":\"value1\",\"item2\":\"value2\",\"some\":\"whoaevaNew\"}');";
$response = Response::make($content, $statusCode);
$response->header('Content-Type', 'application/javascript');
return $response;
}
variable content can also be filled with a view:
$content = View::make('tools/jsonp_resonse'); // also possible with view
Hope that will help someone...
you can use this method provided by laravel
return Response::jsonp($callback, $data,$status, $header);
I'm using Zend Framework 1.x for my project. I want to create a Web service return only JSON string for the caller function. I tried to use Zend_Controller_Action and applied those ways:
1.
$this->getResponse()
->setHeader('Content-type', 'text/plain')
->setBody(json_encode($arrResult));
2.
$this->_helper->getHelper('contextSwitch')
->addActionContext('nctpaymenthandler', 'json')
->initContext();
3.
header('Content-type: application/json');
4.
$this->_response->setHeader('Content-type', 'application/json');
5.
echo Zend_Json::encode($arrResult);
exit;
6.
return json_encode($arrResult);
7.
$this->view->_response = $arrResult;
But when I used cURL to get result, it returned with JSON string surrounded by some HTML tags. Then I tried to user Zend_Rest_Controller with the options above. It still did not success.
P.S.: Most of those ways above are from the question which had been asked on Stack Overflow.
I Like this way!
//encode your data into JSON and send the response
$this->_helper->json($myArrayofData);
//nothing else will get executed after the line above
You need to disable the layout and view rendering.
Explicit disable layout and view renderer:
public function getJsonResponseAction()
{
$this->getHelper('Layout')
->disableLayout();
$this->getHelper('ViewRenderer')
->setNoRender();
$this->getResponse()
->setHeader('Content-Type', 'application/json');
// should the content type should be UTF-8?
// $this->getResponse()
// ->setHeader('Content-Type', 'application/json; charset=UTF-8');
// ECHO JSON HERE
return;
}
If your using the json controller action helper you need to add a json context to the action. In this case the json helper will disable the layout and view renderer for you.
public function init()
{
$this->_helper->contextSwitch()
->addActionContext('getJsonResponse', array('json'))
->initContext();
}
public function getJsonResponseAction()
{
$jsonData = ''; // your json response
return $this->_helper->json->sendJson($jsonData);
}
Your code would need to disable the layout as well in order to stop the content being wrapped with the standard page template. But a much easier approach would just be:
$this->getHelper('json')->sendJson($arrResult);
the JSON helper will encode your variable as JSON, set the appropriate headers and disable the layout and view script for you.
It is much easier.
public function init()
{
parent::init();
$this->_helper->contextSwitch()
->addActionContext('foo', 'json')
->initContext('json');
}
public function fooAction()
{
$this->view->foo = 'bar';
}
I setup the parseExtension for json in my cakephp 2.3.0. No errors are display. It works?
How can I test is?
In RoR ist ist very easy to test via
https://mydomain.de/view/4.json
How does it run on cakephp?
My View-Action is this.
public function view($id = null) {
if (!$this->Atla->exists($id)) {
throw new NotFoundException(__('Invalid atla'));
}
$options = array('conditions' => array('Atla.' . $this->Atla->primaryKey => $id));
$this->set('atla', $this->Atla->find('first', $options));
$this->Atla->id = $id;
$result = $this->Atla->read();
$this->response->type('json');
$this->response->body(json_encode($result));
return $this->response;
$this->set(compact('atlas'));
}
Why i always get an json-request?
If you use the _serialize key cakephp can automatically create json and xml views for you. I typically use the following to create json or xml views:
public function view() {
// data I want to display
$record1 = $this->ModelName->find('first', ...);
$this->set('record1', $record1);
$record2 = $this->ModelName->find('first', ...);
$this->set('record2', $record2);
// option 1: serialize the hard way
$this->set('_serialize', array('record1', 'record2'));
// option 2: serialize the easy way
$this->set('_serialize', array_keys($this->viewVars));
}
PS: the code after your return statement will never be executed.
you'll have create the view
app/View/Atlas/json/view.ctp
which is the view that is being used for .json requests.
Requests without .json will use the regular view file:
app/View/Atlas/view.ctp
There's more information on creating/using JSON and XML views here:
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
From that page the view.ctp may contain something like;
// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));
However, it really depends on what you're trying to achieve. If you will only use the 'Atlas/view' action for JSON responses, and won't be using HTML at all, sometimes you can get away with generating the response-body inside your Controller. Not very much 'in line' with MVC conventions, but it saves you from creating a view that does nothing more than echo json_encode($data); ;)
public function view($id)
{
$this->MyModel->id = $id;
$result = $this->MyModel->read();
$this->response->type('json');
$this->response->body(json_encode($result));
//Return reponse object to prevent controller from trying to render a view
return $this->response;
}
If you do want to use both 'HTML' and 'JSON', depending on the request (with/without .json extension), you should have two view files; 1 for JSON and 1 for HTML;
// This view will be used for your JSON requests
app/View/Atlas/json/view.ctp
// This view will be used for non-JSON (html) requests:
app/View/Atlas/view.ctp
In the json-view, output the data using json_encode(.....);
In the 'normal'/html view, just output normal data
In your controller, set the data as normal
public function view($id = null) {
$this->Atla->id = $id;
if (!$this->Atla->exists()) {
throw new NotFoundException(__('Invalid atla'));
}
$this->set('atla', $this->Atla->read());
}