Get POST body data in Silex RESTful API - symfony

I am creating a RESTful API using Silex. To test I am using Chrome's "Simple REST Client" addon.
In the addon I set the URL to: http://localhost/api-test/web/v1/clients
I set the "method" to: POST
I leave the "headers" blank
I set the "data" to: name=whatever
In my "clients.php" page I have:
require_once __DIR__.'/../../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app->post('/clients', function (Request $request) use ($app) {
return new Response('Created client with name: ' . $request->request->get('name'), 201);
}
In the addon, the output shows: "Status: 201" (correct), a bunch of headers, and "Data: Created client with name: " (it should say "Data: Created client with name: whatever"
What am I doing wrong? I also tried: $request->get('name')
Thank you.

Three steps were needed to resolve:
1) In "Simple Rest Client" set the "Headers" to:
Content-Type: application/json
2) Change the "Data" to:
{ "name": "whatever" }
3) In Silex add the code to convert input to JSON, as described at http://silex.sensiolabs.org/doc/cookbook/json_request_body.html:
$app->before(function (Request $request) {
if (strpos($request->headers->get('Content-Type'), 'application/json') === 0) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
});
Then I was able to access the data in my PHP code with:
$request->request->get('name')
Thank you #xabbuh for your help, which led me towards the answer.

Related

Symfony : Access-Control-Allow-Origin ('security.token_storage)

I work with Symfony and I try to get the user id from another website (ajax). But it does not works !
I have this error in my browser :
Access-Control-Allow-Origin
My code is :
$response->headers->set('Access-Control-Allow-Origin', '*');
/* That does not works and returns : 'NOOOO !!!' but, no error */
if($this->get('security.authorization_checker')->isGranted('ROLE_USER'))
{
$usertosend = $this->get('security.token_storage')->getToken()->getUser();
$id = $usertosend->getId();
$response->setData(array('user'=>$id));
}
else
{
$response->setData(array('user'=>'NOOOO !!!'));
}
/* That works :*/
$usertosend = $this->get('security.token_storage')->getToken()->getUser();
/* That does not works and returns : Access-Control-Allow-Origin error*/
$id = $usertosend->getId();
As soon as I use a getter of the user, there is an error.
If I don't use the getter, there is no error.
There is no problem with the origin domain.
Ok, The problem came from the cookies. So I add these headers :
$response->headers->set('Access-Control-Allow-Origin', 'http://localhost');
$response->headers->set('Access-Control-Allow-Methods', 'get');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, *');
And I add this mention in my ajax method :
xhrFields: {withCredentials: true},
$.ajax({
type: 'get',
url: 'http://myurl.com',
xhrFields: {withCredentials: true},
success: function(user){
alert(user.user);
}
I hope, it will help next users.
Thanks for your help :)
See you
You can use NelmioCorsBundle in your symfony project for adding headers CORS in they response of your application.
This is a very simple and quickly installation. This is a standard bundle for construct a symfony API Rest Application which called with external application.
NelmioCorsBundle

Error while instanciating Restivus "Cannot find name 'Restivus' "

I imported restivus using :
meteor add nimble:restivus
And while using Restivus I encounter this error on meteor startup :
"Cannot find name 'Restivus' ".
I can although GET requests but I wonder if it impacts the behavior of the app.
Here is the code used :
if (Meteor.isServer) {
// Global API configuration
var Api = new Restivus({
apiPath: 'api/',
prettyJson: true
});
}
When receiving POSTs my request.body and my bodyParams are empty :
Api.addRoute(':id/test', {
post: function () {
var id = this.urlParams.id;
console.log("Body contains : ");
console.log(this.bodyParams);
return {
status: 'success',
url : 'post test from id: '+id,
body : this.bodyParams
};
}
});
Does anyone know how to make this error disappear and if this is linked to the POST body problem ?
If you use Meteor 1.4+ you can try to import Restivus to your file with something like this:
import Restivus from 'nibmle:restivus';
The problem with post body being empty was caused by the request I made :
I wasn't specifying the Content-type header.
Once I specified the "Content-Type": "application/json" it worked.
The "Cannot find 'Restivus' " Error is still here though.
Your code looks ok. Here is some code from a server-only file that I am using:
// Global API configuration
var Api = new Restivus({
useDefaultAuth: true,
prettyJson: true,
apiPath: 'restAPI/',
defaultHeaders: { 'Content-Type': 'application/json;encoding="UTF-8"' }
});
// Generates: GET, POST on /api/items and GET, PUT, DELETE on
// /api/items/:id for the Items collection
Api.addCollection(Policy);
Perhaps you should move your code to the server directory? I am on Meteor 1.3.4.

Symfony2, Swiftmailer, Mandrill get response

We've build an email system using Swiftmailer and Mandrill. It works really great but now we would like to integrate the webhooks to trigger alerts on bounces / failures / ...
At this moment we add a custom header with an unique id to each email sent and find our way back when the webhook triggers.
It works well but mandrill already uses an _id that we could use so that we do not add 'another' unique ID on top of that.
Mandrill responds with something like:
[
{
"email": "recipient.email#example.com",
"status": "sent",
"reject_reason": "hard-bounce",
"_id": "abc123abc123abc123abc123abc123"
}
]
Is there any way in Swiftmailer to get this response back into Symfony ?
(So that we can read and store that _id for later use)
I know we could use the Mandrill php SDK but preferably we would like to keep on using Swiftmailer.
EDIT
We are using SMTP Transport with a basic Swiftmailer instance as explained here
<?php
include_once "swift_required.php";
$subject = 'Hello from Mandrill, PHP!';
$from = array('you#yourdomain.com' =>'Your Name');
$to = array(
'recipient1#example.com' => 'Recipient1 Name',
'recipient2#example2.com' => 'Recipient2 Name'
);
$text = "Mandrill speaks plaintext";
$html = "<em>Mandrill speaks <strong>HTML</strong></em>";
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername('MANDRILL_USERNAME');
$transport->setPassword('MANDRILL_PASSWORD');
$swift = Swift_Mailer::newInstance($transport);
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
if ($recipients = $swift->send($message, $failures))
{
echo 'Message successfully sent!';
} else {
echo "There was an error:\n";
print_r($failures);
}
?>
I don't think Mandrill supports passing this reply via SMTP, you'll have to use API for that.
For example, in accord/mandrill-swiftmailer there is a method that returns response from Mandrill: https://github.com/AccordGroup/MandrillSwiftMailer/blob/master/SwiftMailer/MandrillTransport.php#L215
You can get Mandrill's response using following code:
$transport = new MandrillTransport($dispatcher);
$transport->setApiKey('ABCDEFG12345');
$transport->setAsync(true); # Optional
$response = $transport->getMandrillMessage($message);
// $response now contains array with Mandrill's response.
You can integrate it with Symfonu using accord/mandrill-swiftmailer-bundle and after that you can do:
$response = $mailer->getTransport()->getMandrillMessage($message);

How to get site's name from WP API

I'm trying to get WordPress website title using javascript and WP API plugin
I didn't find any example on how to get the site's name but I found the variable name under the entities section in the developer guide
function _updateTitle(documentTitle) {
document.querySelector('title').innerHTML =
documentTitle + ' | '+ $http.get('wp-json/name');
}
The output string of $http.get('wp-json/name') is [object Object]
Does anyone know how to use fix this?
You didn't get enough context. What's $http? What happens when you go to wp-json/name directly in your browser? Here's what I see:
[{
"code":"json_no_route",
"message":"No route was found matching the URL and request method"
}]
Here's a simple example to get you the title:
var siteName;
$.getJSON( "/wp-json", function( data ) {
siteName = data.name;
});
See more elegant solution here https://wordpress.stackexchange.com/a/314767/94636
response will not contain extra data like:
authentication: []
namespaces: ["oembed/1.0", "akismet/v1", "acf/v3", "wp/v2"]
routes: {/: {namespace: "", methods: ["GET"],…},…}
timezone_string: ""
...
_links: {help: [{href: "http://v2.wp-api.org/"}]}

Symfony2 styled emails best practices

What are the best practices to send emails from html & css? I have much mails in my projects & need the solution, that can allow me not to write all below code again & again:
$msg = \Swift_Message::newInstance()
->setSubject('Test')
->setFrom('noreply#example.com')
->setTo('user#example.com')
->setBody($this->renderView('MyBundle:Default:email1.text.twig'));
$this->get('mailer')->send($msg);
Maybe my answer can help. There is a special bundle Symfony2-MailerBundle that render email body from template and allows to set up sending parameters in config file & you won't have to pass them every time you want to build & send email.
Set that code as a function in a service. Functions are for that.
To create a service see the link below.
How to inject $_SERVER variable into a Service
Note: don't forget to inject the mail service as an argument!
arguments: ["#mailer"]
After you set your service;
public function sendMail($data)
{
$msg = \Swift_Message::newInstance()
->setSubject($data['subject'])
->setFrom($data['from'])
->setTo($data['to'])
->setBody($this->renderView($data['view']));
$this->mailer->send($msg);
}
And you can call your service like;
// this code below is just for setting the data
$data = [
'subject' => 'Hello World!',
'from' => 'from#address.com',
'to' => 'to#address.com',
'template' => 'blabla.html.twig'
];
// this code below is the actual code that will save you
$mailer = $this->get('my_mailer_service');
$mailer->sendMail($data);

Resources