I've started sending mails through Laravel (4.2) and its embedded Mandrill driver these days, but I need to catch Mandrill's response somehow.
Here's the code I use to send the message:
Mail::queue('emails.customerspromo', array('messaggio'=>$content, 'disclaimer'=>$disclaimer, 'user_email'=>$to, 'user_id'=>$uid), function($message) use ($sender, $to, $subject) {
$message->from('my#address.it', $sender);
$message->to($to);
$message->subject($subject);
$message->setCharset('UTF-8');
$message->getHeaders()->addTextHeader('X-MC-GoogleAnalytics', 'www.my-site.it');
$message->getHeaders()->addTextHeader('X-MC-GoogleAnalyticsCampaign', 'my-campaign');
});
What I need to intercept is Mandrill's JSON response, for example:
[
{
"email": "destination#address.com",
"status": "sent",
"_id": "80e1ca49d3ed4cbb9d9a3d932c0a14f8",
"reject_reason": null
}
]
How can I do that using Laravel's integrated drivers for Mandrill?
I could use Mail::send instead of Mail::queue, if it's necessary to interpret the response in real time.
Put a variable before your mail function like this:
$response = Mail::queue('emails.customerspromo', array('messaggio'=>$content, 'disclaimer'=>$disclaimer, 'user_email'=>$to, 'user_id'=>$uid), function($message) use ($sender, $to, $subject) {
$message->from('my#address.it', $sender);
$message->to($to);
$message->subject($subject);
$message->setCharset('UTF-8');
$message->getHeaders()->addTextHeader('X-MC-GoogleAnalytics', 'www.my-site.it');
$message->getHeaders()->addTextHeader('X-MC-GoogleAnalyticsCampaign', 'my-campaign');
});
It will still work but now you can see the response from mandrill.
Related
I am writing an API to consume data from another 3rd party application (RapidPro). Sadly, the third party application sends its JSon enclosed in single quotes which Laravel does not seem to understand, when I return the $request object, I get an empty array [].
Is there any way I can accept the JSon as it is with single quotes as I cannot possibly change the third party API?
The JSon to parse is this one:
{
'contact': {
'uuid': 'e65ef92b-24ce-459b-a8fd-beb042330eb0',
'name': 'UserName',
'urn': 'tel: +12000000000'
},
'flow': {
'name': 'MyFlow',
'uuid': 'da5d6c42-a818-481b-b91c-e9622dafe8be'
},
'path': [],
'results': {},
'run': {
'uuid': '2a2f709f-d114-413c-a865-d960cea73981',
'created_on': '2018-05-23T19: 06: 03.308191+00: 00'
},
'input': {
'urn': 'tel: +12000000100',
'text': 'What I wanna say',
'attachments': []
}
}
I user the following route in receiving the API call, it works and takes it to the controller:
Route::post('sms', 'UserController#sms')->name('sms.store');
The SMS controller for this demo let's just write it to a local file and send back the results to be viewed in Insomnia where the demo call is made:
public function sms(Request $request)
{
Storage::put('sms.txt', $request);
return $request;
}
When I post the JSON in via Insomnia to my server I get an empty response, when I use double quotes however; the posted JSON is thrown back nicely.
Turns out, in RapidPro debugger the jSON is shown using single-quotes but in actual POST it uses double quotes. What I had wrong was the AUTH. I had to include some header for Laravel's Basic Once authentication.
I'm trying to build a little Angular 4 Application with a rest api build with slim php. I'm having touble to access the POST variables on specific routes, while it works perfectly on at least one route. Here is my code:
// Log In A League { "email": $email, "pass": $pass }
$app->post('/api/users/league-login', function(Request $request, Response $response){
require "user_management/league_login.php";
$logIn = new LeagueLogIn();
$logIn->logIn($request->getParam("email"), $request->getParam("pass"));
});
Angular sends this JSON:
Object { email: “test#test.com", pass: "000" }
This works perfectly. But on this rout I can't access the POST variables with $request->getParam()
// Add Team To A League { "name": $name, "email": $email }
$app->post('/api/league/team', function(Request $request, Response $response){
require "user_management/team_create.php";
$user = authorizeToken($request->getHeader('Authorization'));
$teamCreate = new TeamCreate();
$teamCreate->create($request->getParam("name"), $request->getParam("email"), $user);
});
Angular sends this JSON
Object { name: "name", email: "test2#test.com" }
As soon as I add double quotes on the keys manually for the request:
Object { "name": "name", "email": "test2#test.com" }
it works perfectly.
Does somebody know what kind of problem this is, or what I'm overlooking?
I try to send this json to my php server {"obj":{"r": "hello", "u": "info#email.com", "p": "123abc"}}.
When I send that, my server receives {"obj":{"r": "hello", "u": "info#email_com", "p": "123abc"}}, I don't understand why.
Here is my angular code:
data = {"obj":{"r": "hello", "u": "info#email.com", "p": "123abc"}};
postMethod(data): Observable<any> {
let headers = new Headers();
this.createAuthorizationHeader(headers);
console.log(JSON.stringify(data));
return this._http.post(this.serverUrl, JSON.stringify(data), {headers: headers})
.map((response:Response) => response.json())
.catch(this.handleError);
}
You said you are using a PHP Server.
Even if you are sending a POST request, if you send pure json, you should check over the raw input stream of PHP like:
$stuff = file_get_contents("php://input");
With the data you obtained from this, you can then decode data and then do your things.
Two options:
Don't JSON.stringify the data in the second parameter. Angular will handle the content-type.
Manually set the content-type header to 'application/json'.
I've read other posts that have similar 404 errors, my problem is that I can correctly query the JSON data, but can't save without getting this error.
I'm using Angular's $resource to interact with a JSON endpoint. I have the resource object returning from a factory as follows:
app.factory('Product', function($resource) {
return $resource('api/products.json', { id: '#id' });
});
My JSON is valid and I can successfully use resource's query() method to return the objects inside of my directive, like this:
var item = Product.query().$promise.then(function(promise) {
console.log(promise) // successfully returns JSON objects
});
However, when I try to save an item that I've updated, using the save() method, I get a 404 Not Found error.
This is the error that I get:
http://localhost:3000/api/products.json/12-34 404 (Not Found)
I know that my file path is correct, because I can return the items to update the view. Why am I getting this error and how can I save an item?
Here is my data structure:
[
{
"id": "12-34",
"name": "Greece",
"path": "/images/athens.png",
"description": ""
},
...
]
By default the $save method use the POST verb, you will need to figure out which HTTP verbs are accepted by your server en order to make an update, most modern api servers accept PATCH or PUT requests for updating data rather than POST.
Then configure your $resource instance to use the proper verb like this :
app.factory('Product', function($resource) {
return $resource('api/products.json', { id: '#id' }, {'update': { method:'PUT' }});
});
check $resource docs for more info.
NOTE: $resource is meant to connect a frontend with a backend server supporting RESTful protocol, unless you are using one to receive data & save it into a file rather than a db.
Otherwise if you are only working with frontend solution where you need to implement $resource and have no server for the moment, then use a fake one, there is many great solutions out there like deployd.
You probably don't implement POST method for urls like /api/products.json/12-34. POST method is requested from angular for saving a new resource. So you need to update your server side application to support it and do the actual saving.
app.factory('Product', function($resource) {
return $resource('api/products.json/:id', { id: '#id' });
});
Try adding "/:id" at the end of the URL string.
Folks,
Designing my first API in Node.JS using restify.js. My background is not webapis, pardon my amateur questions. In any case, I would like to have the res.send(data); responses to comply with the http://jsonapi.org/format/ so that my mobile application can start utilizing the api calls. At the moment if you were to call my api, it would return data in the following format:
{"Count":1,"Items":[{"dbsource":{"S":"foo"},"id":{"S":"5002820"},"name":{"S":"fnameblah,lnameblah"},"expiration":{"S":"06/13/2015"},"type":{"S":"bar"}}]}
Actually what you see above is just a return of a DynamoDB Query call.
So the question is... do you use a special library that you can pass data to, which would format and return the data in JSON format. Which in turn you can return it via res.send(data) to the clients, or is it up to us to make 'data' JSON compliant, then return it? At the end of the day we all want the results to look like:
{
"posts": [{
"id": "1",
"title": "Rails is Omakase",
"links": {
"author": "9",
"comments": [ "5", "12", "17", "20" ]
}
}]
}
Thanks!
In server side, stringify JSON object,
//...
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data)); //data is JSON object
res.end();
In client side, parse JSON string accordingly.
EDIT: Corrected response content type.
JSON data from server should be a JSON string
You have to parse it back the JSON format in client.
JSON.parse(string); // return JSON object