Unable to fetch data from curl request in my express app? - node.js

I am sending curl request from a laravel app to express app. When i trigger curl request then it comes to my express app but i am not able to fetch any data out of it. req.body is empty. Here is the curl request which i am sending from my laravel app. $data contains array of objects.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://some.com/event_upload_manager");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
In my express app, code goes like this
exports.event_uploader = function(req,res){
console.log("Curl Received");
console.log("Body : "+JSON.stringify(req.body));
console.log("Params : "+JSON.stringify(req.params));
console.log("Query : "+JSON.stringify(req.query));
}
I am getting Curl Received then empty request.

Problem is solved , i just added application/x-www-form-urlencoded as header to my curl request and it came,
Besides i added json parser as a middleware for my route in express app.

Related

How to convert docx bytes to file Node Js with axios

I am retrieving a docx file from an endpoint a trying to create a file from it. I get a file but its not readable. maybe it's a problem with the format I am recieving the file.
this is how I retrieve the information a create the file:
const response = await axios.post(
`${process.env.TEMPLATE_HOST}template/data/export`,
{dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);
and this is part of the data I recieve
��_rels/.relsP9��Ruڽ�w
��"[Content_Types].xmlP9��R����>~ ��:docProps/core.xmlP9��R��?;��word/theme/theme1.xmlP9��R!��k� ��
docProps/app.xmlP9��R��HR� ���
word/fontTable.xmlP9��R�����
���word/settings.xmlP9���֏� ��_word/webSettings.xmlP9��R��qK��9 ��Sword/styles.xmlP9��R���XI� ���word/document.xmlP9��R
�wi� ���qword/numbering.xmlP9��R�тsf ���vword/_rels/document.xml.rels9��R�тsf�0xword/_rels/header1.xml.relsP9��R�тsf��yword/_rels/footer1.xml.relsP9��R�тsf��{word/_rels/header2.xml.relsP9��R�тsf�=}word/_rels/footer2.xml.relsP9��R�тsf��~word/_rels/header3.xml.relsP9��R�тsf���word/_rels/footer3.xml.relsP9��R����G$�% ��J�word/media/image1.jpegP9��R��CX�d ��զword/endnotes.xmlP9��Ru�/s�j ����word/footnotes.xmlP9��R���?� ����word/header1.xmlP9��R��
]�� ���word/header2.xmlP9��R���?� ���word/header3.xmlP9��R��>�>� ��g�word/footer1.xmlP9��R�k�^�t ���word/footer2.xmlP9��R��>�>� ���word/footer3.xmlPKj�
I've tried to do it with php and it creates the file correctly. the php file size is 47KB and the nodejs is 83KB, but compare part of the information and looks the same.
$fp = fopen('data.docx', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $templateURl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
echo strlen($server_output);
fwrite($fp, $server_output);
fclose($fp);
any help is appreciated thanks.
Not sure about this, but this could be because of some option that needs to be set on axios.
Maybe try :
const response = await axios.request(
responseType: 'stream',
url: `${process.env.TEMPLATE_HOST}template/data/export`,
method: 'post',
data: {dataId, clientId},
)
fs.writeFileSync(F.path.public(rowFiltered[0]["Files(s) Name"]), response.data);

Contact form 7 wordpress post form to different domain gives access control allow origin error

I am trying to send contact us form details to Insightly CRM with Web to Lead html code. I changed action url of the form with the following code in my functions.php:
add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url) {
global $post;
$id_to_change = 1315;
if($post->ID === $id_to_change)
return 'https://xxxx.insight.ly/WebToLead/Create';
else
return $url;
}
Everything looks fine on inspector but i get the following error on submit:
XMLHttpRequest cannot load https://xxxx.insight.ly/WebToLead/Create. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxxx.com' is therefore not allowed access.
I tried adding this to my functions.php:
add_action( 'init', 'allow_origin' );
function allow_origin() {
header("Access-Control-Allow-Origin: *");
}
I tried adding this in theme.php:
header("Access-Control-Allow-Origin: *");
I tried adding this to scripts.js of contact form 7 plugin:
$.ajax({
url: url,
++headers: { 'Access-Control-Allow-Origin': '*' },
++crossDomain: true,
I tried adding this to .htaccess:
Header always set Access-Control-Allow-Origin "*"
Nothing works :(
My server has Varnish 4 and ConfigServer Security & Firewall but i disabled both and still get the same error.
Please help me :(
After my research I noticed that javascript was the problem and it was not possible to bypass it with the Access-Control-Allow-Origin in any way on my side.
I used curl in a hooked php script so it can send the details to the other domain.
So I added a hook in my functions.php which I am overriding the wpcf7_mail_sent function:
add_filter('wpcf7_mail_sent', 'wpcf7_custom_mail_sent');
function wpcf7_custom_mail_sent($contact_form) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://xxxx.insight.ly/WebToLead/Create");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($_REQUEST));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}

extract user and caption from instagram image URL

I want to extract the user who has posted the photo and caption associated with the photo from the photo URL using instagram API.
For example:
The image URL is: http://instagram.com/p/rRoE9uskcD/
How can I extract user and caption from this ?
You actually only need to use Instagam's Embedding Endpoints
so this link :
http://api.instagram.com/oembed?url=http://instagr.am/p/rRoE9uskcD/
will return the info associated with such media in a json format.
You can get the info programmatically inside an ajax function like:
var url = "http://api.instagram.com/oembed?url=http://instagr.am/p/rRoE9uskcD/";
jQuery(document).ready(function ($) {
$.ajax({
url: url,
dataType: "jsonp", // <== this is important
cache: false,
success: function (data) {
console.log(data.author_name); // the author
console.log(data.title); // the caption (if any)
}
});
});
See JSFIDDLE
If you have setup an Instagram client you could do like this using curl and php:
$instagram_client_secret = 'YOUR_CLIENT_SECRET';
$instagram_client_id = 'YOUR_CLIENT_ID';
$shortcode = 'rRoE9uskcD';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/v1/media/shortcode/$shortcode?client_id=$instagram_client_id&client_secret=$instagram_client_secret");
$result = curl_exec($ch);
$result = json_decode($result);
if(isset($result->meta->code) && $result->meta->code == 200) {
// print out the username and caption
print $result->data->user->full_name;
print $result->data->caption->text;
}
curl_close($ch);
The following example uses jQuery to achive the same thing:
$(document).ready(function() {
$.get('https://api.instagram.com/v1/media/shortcode/rRoE9uskcD?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET', function(data) {
alert(data.data.user.full_name + ' ' + data->data->caption->text);
}, 'jsonp');
});
Reference: Media Endpoints

Node, express API proxy requests to external API with basic auth header

I have built a API using node.js and express.
But i need to be able to proxy some requests on a specific route to a external server and show the response from the external server to the clint doing the request.
But i also need to forward the basic auth that the client is send along with the request.
I have tried using the request module like:
app.get('/c/users/', function(req,res) {
//modify the url in any way you want
var newurl = 'https://www.external.com'
request(newurl).pipe(res),
})
But it seems to not send the basic auth header because i get "403 Forbidden" back form the external server(www.external.com)
The request im making is looking like:
GET http://example.se:4000/c/users/ HTTP/1.1
Accept-Encoding: gzip,deflate
X-version: 1
Authorization: Basic bmR4ZHpzNWZweWFpdjdxfG1vcmV1c2*******=
Accept: application/json
Host: example.se:4000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
And it works if i do the exact same request but against www.external.com directly so there is some issue when doing the proxy in node.
The request module is totally unaware of anything that you don't pass explicitly to it. To set the request headers and copy the response headers as well do the following:
// copy headers, except host header
var headers = {}
for (var key in req.headers) {
if (req.headers.hasOwnProperty(key)) {
headers[key] = req.get(key)
}
}
headers['host'] = 'final-host'
var newurl = 'http://final-host/...'
request.get({url:newurl, headers: headers }, function (error, response, body) {
// debug response headers
console.log(response.headers)
// debug response body
console.log(body)
// copy response headers
for (var key in response.headers) {
if (response.headers.hasOwnProperty(key)) {
res.setHeader(key, response.headers[key])
}
}
res.send(response.statusCode, body)
})
Try explicitly passing in the auth details like so
request(newurl).auth(username, password).pipe(res);
https://github.com/mikeal/request#http-authentication

How do I parse this HTTP POST request in Express?

I'm building my first node.js site with expressjs.
Part of it is handling notifications from Fitbit to an endpoint on my site, /api/1/fitbit_update, like this one:
POST /api/1/fitbit_update HTTP/1.1
Host: myhost.dk
Content-Type: multipart/form-data; boundary=JGUOAeGT3Fjgjcdk6s35F2mPVVyTdzgR
Content-Length: 374
Connection: keep-alive
--JGUOAeGT3Fjgjcdk6s35F2mPVVyTdzgR
Content-Disposition: form-data; name="updates"; filename="update1353963418000.json"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: binary
[{"collectionType":"activities","date":"2012-11-26","ownerId":"qw12er23","ownerType":"user","subscriptionId":"112233-activities"}]
--JGUOAeGT3Fjgjcdk6s35F2mPVVyTdzgR--
I need to parse the json object in the body of the HTTP request, but Express cannot help me with that.
Any ideas how to get a hold of the JSON object?
I've seen some middleware examples, but I'm not sure how to actually get to the contents with the req.on('someevent') approach.
req.body returns an empty object {}
So this request is a multipart file upload where the file you are interested in is a JSON document. Check out the express multipart example. You will want to do something like:
read the file that connect has saved for you at req.files.updates.path
parse that JSON data into an object using JSON.parse
Try req.body in your POST route handler.
Add express.bodyParser()
for older version:
app.express.createServer(
express.cookieParser(),
express.bodyParser(),
.....
);
for newer version:
server.use(express.bodyParser());
Then you can access body by using req.body
Values ​​sent from form using post method: email and password
app.post('/login', function(req, res){
/* Get Errors of Validates */
var errors = [];
req.onValidationError(function(msg) {
errors.push(msg);
//console.log('msg: ' + msg);
return this;
});
/* Validates */
req.sanitize('email').xss();
req.sanitize('password').xss();
req.assert('email', 'Range email').len(6, 40);
req.assert('password', 'Range password').len(6, 20);
//*****************************************************
// Value Send Post "password" --> req.body.password
//*****************************************************
var pass_shasum = crypto.createHash('sha256').update(req.body.password).digest('hex');
....
Regards.

Resources