-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content-Type doesn't work correctly #15
Comments
app.use((req, res, next) => {
req.get('Content-Type'); // it returns application/json
}); But this one doesn't work app.use(bodyParser.json());
---- OR ----
app.use(bodyParser.json({ type: 'application/json' })); |
So, comparing local execution with azure, I can say that |
When I started with this library my first test is doing a post. I couldn't figure out why it was hanging. A search found your issue and I was saved by using Now I am testing file upload which was using bodyparser with type |
if (process.env.CLOUD === 'azure') { // For azure cloud apply specific body parser
app.use(azureBodyParser());
} else {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
} So here I check first the environment. If it's not azure then just follow usual express bodyParser style. For azure environment I created custom middleware to handle request object. 'use strict';
const queryString = require('query-string');
/**
* Azure body parser
*/
function azureBodyParser() {
return function(req, res, next) {
// x-www-form-urlencoded
if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
req.body = queryString.parse(req.body, { arrayFormat: 'bracket' });
}
next();
};
}
module.exports = azureBodyParser; This can be different. You can add more checks if you want. For your case with multer I didn't try anything actually. I assume data will come as buffer object, and you should add one more check for content-type |
I opened a PR which changes the IncomingMessage (wrapped req object) to be a Readable stream with the rawBody pushed. This seems to fix this issue |
Thanks @dcollien . Hope it will. Here is some useful information about this ticket I've found so far https://stackoverflow.com/questions/50043746/azure-function-doesnt-respond-if-content-type-application-json |
@iredjee I think it might not so much to do with the content-type, but the same cause as #22 - if the request object isn't a stream, then it can't pipe the original request body into a stream-consuming JSON parser. The original code just did a NOOP if it tried to be used as a stream - hence why it might have just timed out. Changing the request object to extend a stream (as it is in express) might be the fix (I hope). edit: if you'd like to try it out, you can change your package.json to use:
|
I'm trying to make POST request and send JSON data to Azure Function using
application/json
Content-Type. But the function call hangs and return timeout instead of result.App has the following configuration
Locally such expressjs application works perfectly. But looks like some trouble with Content-Type on Azure.
However when I change configuration to this one
My POST requests pass. But for
x-www-form-urlencoded
it still can't parse the data and returns timeout.Could you please help me to understand why it doesn't work? Thank you
The text was updated successfully, but these errors were encountered: