I have managed to make middleware work in Strapi. But, I can't see the body in the request.
Inside /middlewares/getEmail/index.js, I Have
module.exports = (strapi) => {
return {
initialize: function (cb) {
strapi.app.use(async (ctx, next) => {
if (ctx.method === "POST" && ctx.url === "/email-leads") {
console.log(ctx);
}
await next();
});
},
};
};
and ctx request logs:
request: {
method: 'POST',
url: '/email-leads',
header: {
'content-type': 'application/json',
accept: 'application/json',
'user-agent': 'PostmanRuntime/7.26.8',
'postman-token': 'xxx',
host: 'localhost:1337',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
'content-length': '33'
}
},
This is the only middleware I have written on this application. In the /config/middleware.js, I have
module.exports = {
load: {
before: ["getEmail", "responseTime", "logger", "cors", "responses", "gzip"],
order: ["parser"],
after: ["router"],
},
settings: {
getEmail: {
enabled: true,
},
},
};
I read about this koa-body/unparsed.js to read the body but there's literally no body in the ctx.request. Thanks for help.
if you console.log(ctx.request.body); before await next(); line it will prints undefined because body is not loaded yet. so you have 2 option for fetching body.
Use console.log(ctx.request.body); after await next(); line
Load body from raw node request like below example
Click Here
So, I have reached a solution. Posting here in case anyone else needs it.
In config/middleware.js, I have changed the load, taking the custom middleware to after array.
load: {
before: ["responseTime", "logger", "cors", "responses", "gzip"],
after: ["parser", "router", "getEmail"],
},
I still can't see the ctx.request.body if I log ctx or ctx.request. But, if I use ctx.request.body directly, I can reach it in case the load is written as above (or custom middleware after parser).
Related
I am trying to upload some videos to Cloudflare Stream API. Here is official documentation and its example request using curl: https://developers.cloudflare.com/stream/uploading-videos/upload-video-file/
I am doing the request in Node.js
const uploadVideo = (video: Express.Multer.File): => {
const formData = new URLSearchParams();
formData.append('file', video);
let cloudflareResponse;
try {
cloudflareResponse = await axios.post(
`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/stream/copy`,
formData,
{
headers: {
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'multipart/form-data',
//'Tus-Resumable': '1.0.0',
//'Upload-Length': '600',
//'Upload-Metadata': 'maxDurationSeconds 600'
}
}
);
} catch (e) {
console.log('Error while trying to upload video to Cloudflare API ', e);
}
}
The commented Headers I took from this article, in which the request is done in Django and I tried to replicate it https://medium.com/#berman82312/how-to-setup-cloudflare-stream-direct-creator-uploads-correctly-802c37cbfd0e
The error I am getting is a 400 and here is some of the response
config: {
url: 'https://api.cloudflare.com/client/v4/accounts/...../stream/copy',
method: 'post',
data: 'file=%5Bobject+Object%5D',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'multipart/form-data',
Authorization: 'Bearer .....',
'Tus-Resumable': '1.0.0',
'Upload-Length': '600',
'Upload-Metadata': 'maxDurationSeconds 600',
'User-Agent': 'axios/0.21.1',
'Content-Length': 24
},
data: { result: null, success: false, errors: [Array], messages: null }
I am sure something is wrong in the request and hope someone could help me spot the mistake or suggest some modifications that might help. I have been stuck with this problem for hours and on Postman I am also getting a 400 response when trying to send with form-data.
I have a node.js application served over https. I would like to call an API from that application. The API is also served over https and it has been generated using the express-generator.
Unfortunately the call never works. There is no error message. The call never reaches the API.
Strangely enough if I try to call another public API (e.g. https://api.publicapis.org/entries') that is working perfectly.
Here is my call:
const requestBody = {
'querystring': searchQuery,
};
const options = {
rejectUnauthorized: false,
keepAlive: false, // switch to true if you're making a lot of calls from this client
};
return new Promise(function (resolve, reject) {
const sslConfiguredAgent = new https.Agent(options);
const requestOptions = {
method: 'POST',
body: JSON.stringify(requestBody),
agent: sslConfiguredAgent,
redirect: 'follow',
};
fetch('https://192.168.112.34:3003/search', requestOptions)
.then(response => response.text())
.then(result => resolve(result))
.catch(error => console.log('error', error));
});
};
And here is the API which I would like to call:
router.post('/', cors(), async function(req, res, next) {
req.body;
queryString = req.body.querystring;
let data = JSON.stringify({
"query": {
"match": {
"phonetic": {
"query": queryString,
"fuzziness": "AUTO",
"operator": "and"
}
}
}
});
const { body } = await client.search({
index: 'phoneticindex',
body: data
});
res.send(body.hits.hits)
});
What is wrong with my API and/or the way I am trying to communicate with it?
UPDATE: I receive the following error in the fetch catch block: 'TypeError: Failed to fetch'
When I create a request in Postman I receive the expected response.
UPDATE 2: This is most probably an SSL related issue. The webapp is expecting an API with a valid certificate. Obviously my API can only have a self signed cert which is not enough here. How can I generate a valid cert for an API which is running on the local network and not publicly available?
UPDATE 3: I managed to make it work by changing the fetch parameters like this:
fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
mode: 'cors',
body: raw,
agent: httpsAgent,
redirect: 'follow',
})
and on the API side I added the following headers:
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : 'https://localhost:2200',
'Access-Control-Allow-Methods' : 'POST',
'Access-Control-Allow-Headers' : 'Content-Type, Authorization'
I also added app.use(cors()) and regenerated the self-signed certificates.
I have a problem with my api call using NodeJS. I have no problem with postman, but when I run it with Node, server response is 401.
Here's the nodejs code:
var request = require("request");
var options = { method: 'GET',
url: 'https://api.cardmarket.com/ws/v2.0/output.json/products/find',
qs:
{ search: 'Salamangrande%25Loup%25Du%25Soleil',
idGame: '3',
idLanguage: '2' },
headers:
{ 'cache-control': 'no-cache',
Connection: 'keep-alive',
Cookie: 'PHPSESSID=m399v2el9635i3jq0e4did8f5k',
'Accept-Encoding': 'gzip, deflate',
Host: 'api.cardmarket.com',
'Postman-Token': '9b19a85d-8888-4f31-8d24-fe309a55bf76,4d8f4741-4b85-4d5e-85dc-8ac43ea5f56c',
'Cache-Control': 'no-cache',
Accept: '*/*',
'User-Agent': 'PostmanRuntime/7.19.0',
Authorization: 'OAuth realm="https%3A%2F%2Fapi.cardmarket.com%2Fws%2Fv2.0%2Foutput.json%2Fproducts%2Ffind",oauth_consumer_key="xxxxxxxxx",oauth_token="xxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1579186959",oauth_nonce="vWUKBoTTkle",oauth_version="1.0",oauth_signature="ZD2TwzLVHqOjLm3u%2BWv%2FsQ8mdfs%3D"' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(response.statusCode);
});
So basically here's my (perfectly working) .
Any idea what's wrong with the syntax or how to properly implement the oauth signature in the headers ?
HTTP 401 error means "unauthorized".
I don't think OAuth authorization is meant to be passed through the headers.
It is supposed to be something like :
var request = require("request");
url='https://api.cardmarket.com/ws/v2.0/output.json/products/find';
oauth = {
consumer_key: CONSUMER_KEY,
consumer_secret: CONSUMER_SECRET,
token: AUTH_TOKEN,
token_secret: TOKEN_SECRET,
signature_method : 'RSA-SHA1',
};
qs = {
search: 'Salamangrande%25Loup%25Du%25Soleil',
idGame: '3',
idLanguage: '2'
};
request.get({url:url, oauth:oauth, qs:qs, json:true}, function (e, r, product) {
console.log(product)
})
Check the request docs for more explanation on how to deal with OAuth in request.
(I strongly suggest using Axios instead of request, but that's up to you)
I am running my React js web app in one port 3000.
For node server I am using 4000.
While calling fetch method it returns `400 Bad request'.
Error
POST http://localhost:4006/auth/admin 400 (Bad Request)
react code npm started in 3000 port
fetch('http://localhost:4000/auth/admin',
{ mode: 'no-cors',
method: "POST",
body: JSON.stringify({
username:"admin",
password:"1234"
}),
headers: {
"Content-Type": "application/json",
'Accept': 'application/json, text/plain, */*',
credentials: "omit", //
// "Content-Type": "application/x-www-form-urlencoded",
},
})
.then((response) => console.log(response));
node code running in 4000 port
const passport = require("passport");
const route = require("../constants/routeStrings");
const keys = require("../config/keys");
const processStatus = require("../constants/processStatus");
const success = {
status: processStatus.SUCCESS
};
const failute = {
status: processStatus.FAILURE
};
module.exports = app => {
app.post('/auth/admin', passport.authenticate("local"), (req, res) => {
res.send(success);
});
};
Do not stringify the body. Change from
body: JSON.stringify({
username:"admin",
password:"1234"
}),
to
body: {
username:"admin",
password:"1234"
},
The 400 response is raised by passport since it is unable to read your params. You need to tell your "node" app to parse them before your actual routes.
// Import body parser, you should read about this on their git to understand it fully
const parser = require('body-parser');
const urlencodedParser = parser.urlencoded({extended : false});
// before your routes
app.use(parser .json());
app.use(urlencodedParser) // This will parse your body and make it available for your routes to use
Then do your other calls.
Also, make sure that you are sending username and password keys, otherwise read the documentation on how to change these key names to something else
I suffered long hours, but I overcame it throw writing those lines of code blocks. I successfully send the request to the server's controller, hopefully yours: make it try.
First define a async function to make POST request:
async function _postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
redirect: 'follow',
referrerPolicy: 'no-referrer',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify(data)
});
return response.json();
}
Now create a request JSON payload:
let requestPayload = {
propertyName1: 'property value1',
propertyName2: 'property value23',
propertyName3: 'property value',
So on
}
Note: Request model will be your desired model, what request payload you actually send.
Now make a request using this payload including your end point URL:
_postData('http://servername/example', requestPayload )
.then(json => {
console.log(json) // Handle success
})
.catch(err => {
console.log(err) // Handle errors
});
100% worked on my project.
I need to make an XML POST (I know don't ask it's government...) and I can't get it to work with node request promise native.
I have tried turning my string of XML into a buffer, String(), .toString etc. The POST works if I turn json:true so I don't think it's a networking issue ( when passed a xml string with json true it sends a json like { 'variablename': 'stringed XML that I want to send as the body' } )
Here is what I'm working with. I've been banging my head for a while here any help appreciated.
Would ideally be promise/async.
Maybe I should look to find a XMLHttp request npm module?
var request_promise_native_options = {
method: 'POST',
uri: 'http://requestbin.fullcontact.com/198flbs1',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'text/xml'
//'Content-Length': Buffer.byteLength(my_xml_string) //I've tried multiple ways to use this
},
body: {
my_xml_string //also tried many ways here Buffer, String() etc
},
json: false // automatically stringifys body to json if true
};
request_promise(request_promise_native_options)
.then(function (response) {
console.log("success");
})
.catch(function (err) {
console.log(err);
})
Kudos to #kevin-b to help me see the obvious. Simply remove {}
var request_promise_native_options = {
method: 'POST',
uri: 'http://requestbin.fullcontact.com/198flbs1',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'text/xml'
'Content-Length': Buffer.byteLength(my_xml_string)
},
body: my_xml_string,
json: false // automatically stringifys body to json if true
};