I am trying to get the domain from the request in nodejs, the request originates from PHP and when I get origin in Nodejs I get undefined.
This is my code in PHP: (domain1.com)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://myNodedomain.com/sendAlert',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('token' => '3242342332'),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
This is my code in NodeJs: (mynodedomain.com)
app.post('/sendAlert', async (req, res) => {
console.log(req.get('host'));
console.log(req.get('origin'));
});
The desired result is:
https://domain1.com/
Any solution?
I'm not super familiar with PHP, but adding something like this might work:
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Origin: domain1.com']);
Related
Hi Developer,
I am working on a website where I want to fetch all my client Flipkart seller's order details to my client website from where they can manage their order, what will be the possible way to integrate it, Will Flipkart allow me to see my Client order details on the website
Thanks!
using this you will get orders
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.flipkart.net/sellers/v2/orders/search/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"filter": {
"orderDate": {
"fromDate": "2022-02-01T00:00:01.19800Z",
"toDate": "2022-02-03T20:43:30.19800Z"
}
}
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer $token',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I wrote a function that receives my list from the server (NODEJS) in the REACT language
getList = () => {
const request = new Request('http://localhost:3000/api/get');
fetch(request, {method: 'GET',mode: 'no-cors',
})
.then((response) => console.log("response", response.JSON()));
}
The request is being made but the returned value I receive in CONSOLE.LOG is:
response Response {type: "opaque", url: "", redirected: false, status: 0, ok:
false, …}body: (...)bodyUsed: falseheaders: Headers {}ok: falseredirected: falsestatus:
0statusText: ""type: "opaque"url: ""__proto__: Response
This is the code on the server:
const MyList=[
{id:1,name:'to do list'},
{id:2,name:'to sleep'},
]
app.get('/api/get',(req,res)=>{
res.send(MyList);
});
Where is the values of the list returned from the server ???
The data from the server can be accessible from response.body, however response.json() returns a promise, so you will have to do something like this:
getList = () => {
const request = new Request('http://localhost:3000/api/get');
fetch(request, {method: 'GET',mode: 'no-cors'})
.then((response) => response.json().then(json => {
console.log(json.body);
});
}
I added .json to response:
getList = () => {
const request = new Request('http://localhost:3000/api/get');
fetch(request, {method: 'GET',mode: 'no-cors' })
.then((response) => console.log("response", response.json()));
}
and received the following error:
TobuyList.js:30 Uncaught (in promise) SyntaxError: Unexpected end of input
at TobuyList.js:30
In your server, change res.send to res.json
On the client side (taken from mozilla docs):
fetch('http://localhost:3000/api/get', { mode: 'no-cors' })
.then(response => response.json())
.then(data => console.log(data));
I have some php code that runs curl commands. However the company I work for is switching over to nodejs and I am not sure how to replicate the request in the same way in node. Or I would even be happy if I could replicate the request on the front end using axios. I don't really care how I do i I just need to replicate the request in the exact same way.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array(
'Authorization: Token '.$token
),
CURLOPT_URL => 'https://myWebsite.com/api/led/contents/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
url => '#bannerStockUpload/'.$assetName.'/'.$assetName.'_'.$dimensions[1].'.mp4',
thumbnail => '#bannerStockUpload/'.$assetName.'/'.$assetName.'_thumb.jpg',
type => 'video',
width => $dimensions[0],
height => $dimensions[1],
duration => $dur,
name => $assetName."_".$dimensions[0]."x".$dimensions[1]."mp4"
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
you can install axios via npm then:
const axios = require('axios');
const data = {
url: '#bannerStockUpload/'.$assetName.'/'.$assetName.'_'.$dimensions[1].'.mp4', // chnage $assetName to js variable
thumbnail: '#bannerStockUpload/'.$assetName.'/'.$assetName.'_thumb.jpg',
type: 'video',
width: $dimensions[0], // change dimension to js variable
height: $dimensions[1], // change dimension to js variable
duration: $dur,
name: $assetName."_".$dimensions[0]."x".$dimensions[1]."mp4"
}
const headers = {
headers: {
Authorization: 'Token ' + $token // add javascript token
}
}
// Make a request for a user with a given ID
axios.post('https://myWebsite.com/api/led/contents/', data, headers)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
I am getting the Authentication_MissingOrMalformed, getting the token properly and assigned in the header.
// Curl call for calling graph API
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.windows.net/xxxx/users/giri#xxxx.com/memberOf?api-version=1.6",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROXY => "xxxxx:8080",
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "api-version=1.0",
// Adding access toker here
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$result->access_token,
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
),
The error information Authentication_MissingOrMalformed it means that the access resource is not match the aud of accesstoken.
As Juunas mentioned that you need to change the resource to https://graph.windows.net during get the accesstoken.
According to GetUsersMemberships API, we could know that content-type should be application/json and the http method is GET.
Content-Type: application/json
So you also need to change content-type from application/x-www-form-urlencoded to application/json.
I'm building simple Angular2 application where a given user can upload videos via Microsoft Azure Media Service REST API.
I'm trying to get a token by using this request:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&resource=https://rest.media.azure.net/&client_id=<application id>&client_secret=<password you selected for authentication>" https://login.microsoftonline.com/<Azure AD Tenant ID>/oauth2/token?api-version=2.11
But it will not work via AJAX (that's because CORS), so is there any other simple way to get an app-only token?
I've created php script which returns token, I think it's fastest solution.
Here is code, maybe will be useful for someone:
<?php
define(APP_CLIENT_ID, '<client_id>');
define(CLIENT_SECRET, '<client_secret>');
define(AD_TENANT_ID, '<tenant_id>');
define(RESOURCE_URL, 'https://rest.media.azure.net');
function curlTokenRequest() {
$url = "https://login.microsoftonline.com/".AD_TENANT_ID."/oauth2/token";
$request = "grant_type=client_credentials&resource=".RESOURCE_URL."&client_id=".APP_CLIENT_ID."&client_secret=".CLIENT_SECRET;
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 200, // timeout on connect
CURLOPT_TIMEOUT => 200, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded"
)
);
curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
http_response_code($httpcode);
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
return $data;
}