View RawBody In SailsJS - json

I'd like to dump out, via sails.log.debug(), the raw POSTed data as seen by a controller function. I am dealing with JSON coming from a third-party that may be badly formatted and need to figure out where/how. I'd like to see the whole, raw dump.
create: function(req, res) {
sails.log.debug(???);
//var ticket = JSON.parse(req.param("webhook"));
return res.echoRequest(true);
}

You will need to use middleware to get the "RAW" body. You will want to grab that pre-sails
Check out answer to this question Node.js - get raw request body using Express

You could use:
var packet = req.params.all();
sails.log.debug(packet);
Hope that helps.

Related

How can I send data (string) from my html to my server (node or express) and execute certain function with it?

I have a node.js server running and is executing what I want it to do, create an excel document with data fetched with Axios from an API, now, I want to allow a user to input a string on my HTML and then send that string to my web server, and perform an Axios request to the API that I am consuming. How can I do that? Should I use a POST request, a PUT request, or anything else?
Thanks!
You should be able to send data from your client to the server via the request body regardless of the request method whether GET/POST/PUT.
fetch call would look like this
// just a simple get request
fetch('/example-endpoint', {
method: 'POST',
body: {
data: 'String to be sent'
}
});
And on your node express server
app.post('/example-endpoint', (req, res) => {
const data = req.body.data;
// do stuff with your data here.
})
GET request method is used for getting data, while POST and PUT are used for creating and updating data, so use them as you need them.
POST request to your server, like uh https://myserver.com/api/${userString}
something like that

How to process Vue/Axios Json payload posted data on Yii2

It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.
I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).
As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.
public $enableCsrfValidation = false;
But no matter what, data was not being added to the request/post data inside Yii2.
The following Image, explains the problem you will find there:
The Axisos method that sends the post with test data.
The Yii2 Action stpoed at the place, I should be able to see data.
The capture of the xdebug variables and data for the request.
The capture of Chrome where you can check the payload is sent.
The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.
After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.
Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.
There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.
This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.
So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:
$raw_data = Yii::$app->request->getRawBody();
Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.
$object= json_decode($raw_data );
And finally use the data inside by calling the properties you look for, sent on the pay load:
Json Payload:
{
"msg":"This is my payload",
"id":"11"
}
To use it:
echo $object->{'msg'}; // prints: This is my payload
So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.

New line in json array is getting converted to a comma | nodejs

I am relatively new to nodejs and running into an issue while parsing a Json post request.
Here is the JSON format of the post request:
{"parameters":{"issuerId":[96409],"source":["'XYZ'"]}}
And here is my code to read it.
function getSearchData(req, res, next) {
console.log("req is" + req.body);
try {
JSON.parse(reqJSON);
} catch (e) {
console.log(e);
}
}
This parsing works fine and I am able to parse it and do my further logic. However, if I change my format of post request(same request with additional new lines) it fails to parse as it adds additional commas in place of each new line in the request.
{
"parameters": {
"issuerId": [96409],
"source":["'XYZ'"]
}
}
Here's the output from the code with the second request.
req is{,"parameters":{"id":[96409],,"source":["'XYZ'"]}}
[SyntaxError: Unexpected token ,]
If you notice, an extra comma gets added at each new line, which was never in the request to begin with.
What am I doing wrong here?
You should never have to parse the JSON yourself, unless you're concatenating the request body stream yourself.
Hint 1: Do you use any framework like Express? Do you use body parser?
Hint 2: How do you create the JSON?
Hint 3: Do you use correct content-type?
Hint 4: How do you create req.body from the request stream?
You didn't include the entire code so it's impossible to give you a specific solution.
What am I doing wrong here?
Whatever you're doing wrong here, it's not included in the question.
However, if I change my format of post request(same request with additional new lines)
It would be useful if you included more details of how you do it.
I see two potential sources of that problem:
either the commas are introduced during the on the client side
or they are introduced during the request reading on the server side
You didn't show us any of those two parts - you didn't show the serializing code and the code that sends the data, and you didn't include the code that gets the data, possibly joins it from chunks and parses the JSON. But the problem is likely in one of those parts.
Update
Here is an example on how to do what you need using Express. You didn't answer whether you use any framework like Express or not, but I think that you should if you can't achieve that simple task without it, so here is a working example:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
function getSearchData(req, res, next) {
console.log('req body is', req.body);
console.log('req body JSON is', JSON.stringify(req.body));
res.json({ ok: true });
}
app.use(bodyParser.json());
app.use(getSearchData);
app.listen(3335, () => console.log('Listening on http://localhost:3335/'));
It shows how to correctly get a parsed JSON request body as an object req.body, how to print the data as a standard console.log representation and serialized again as JSON. See how it works and compare it to your own solution. This is all I can do because not having seen your entire solution I cannot tell you more than the hints that I've already given.

use JSON api for live news

I have this Fox Sport API url :
https://newsapi.org/v1/articles?source=fox-sports&sortBy=top&apiKey=955acf3993df49169dfa33dce76d015f
How do i use this? I know it's based on Json. But where do i put this URL? in what format or file? Can someone please help me?
I alrady tried putting it between scripts tags in my index.php file but no results...
Thank you!
Google it ---> Php curl. Read the doc!
If you are using a framework, let us know which one😃
JSON is simply a useful way to send and receive strings that represent objects in JavaScript. In these days I'm working on an application that uses JSON strings to store and retrieve data, and I found a very simple way to get data from JSON strings, that is jQuery. This library has a jQuery.getJSON() method, which let you to load JSON-encoded data from the server (or locally) using a GET HTTP request. Here you can find all the details you need to use this method.
Obviously you could choose not to use any third-part library and do what you need in vanilla JavaScript, but jQuery is very useful since it helps to avoid common cross-browser issues.
In my application I store data from a JSON string in this way:
var placesList;
jQuery.getJSON("places.txt").done(function (data) {
placesList = data;
});
that is using an external variable to store them using an anonymous function. As you can see, my URL here is places.txt, but you can use any valid URL you want that provide a JSON string.
You should try like this -
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url = 'https://newsapi.org/v1/articles?source=fox-sports&sortBy=top&apiKey=955acf3993df49169dfa33dce76d015f';
$.getJSON(url).then(function(res){
console.log(res) //{status: "ok", source: "fox-sports", sortBy: "top", articles: Array[10]}
console.log(res['status']) //ok
console.log(res['source']) //fox-sports
console.log(res['sortBy']) //top
console.log(res['articles'].length) //10
})
})
</script>

How to send data back to node from casperjs?

How can I send data back to node, from a process launched via execfile in nodeJS? preferably in a JSON format.
This is how my code looks like right now:
//index.js NodeJS
var execFile = require('child_process').execFile;
var express = require('express');
app.get('/', function(req, res) {
var lchecker = execFile('/usr/local/bin/casperjs', [myprogram, myargs]);
lchecker.stdout.on('data', function(data) {
var dataObject = JSON.parse(data); //This throws an error.
});
});
The casperjs script that I'm calling returns JSON-like strings, like this:
console.log("[{href: targetLink.href, anchor: targetLink.anchor, isLive: 0, isFollowed: null}]");
This is the error that I get
When I'm trying to parse the JSON-like string, I get an error that says:
19 Jun 16:46:43 - [nodemon] starting node index.js
undefined:1
[{href: targetLink.href, anchor: targetLink.anchor, isLive: 1, isFollow: 1}]
^
Unexpected token h
So my JSON is invalid, and sincerely, I'm sure that there's a better way to send data back to node from casperjs, but I don't know how.
I've been thinking about creating a new route in express, and then make casperjs visit that route and pass the information via GET, and then manipulate that information in node. Is this a good way to achieve this?
Even though I received good and viable answers, I ultimately ended up outputting everything to stdout in casperjs, to send it back to PHP through a JSON array.
so in casperjs I wrote something like:
console.log(JSON.stringify(targetLink))
And then in node, I could just access that through JSON.parse and manipulate the data in any way I want.
EDIT:
I've run into this situation more often than not, so alternatively you can make CasperJS POST the information to a web endpoint, it's sometimes cleaner, but it adds overhead if you are worried about security and you need to make sure that only authorized scrapers can post data to your endpoint.
I have used CasperJS on NodeJS by running CasperJs as a service.
Basically NodeJS through http.get() makes a request to CasperJS script which return a JSON object as response.
Here an example and more details about how a CasperJS script can start a web server:
CasperJS passing data back to PHP
You may probably prefer to use something like SpookyJS (https://github.com/WaterfallEngineering/SpookyJS) which offer the ability to use CasperJs inside a Node.js program.
I don't know if you will find the feature you want but it's probably cleaner anyway.