question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content-Type doesn't work correctly

See original GitHub issue

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

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

Locally such expressjs application works perfectly. But looks like some trouble with Content-Type on Azure.

However when I change configuration to this one

app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(bodyParser.urlencoded({ extended: true }));

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

5reactions
ghostcommented, Jun 21, 2018

application/*+json is not actually the way. I did some trick to manage bodyParser stuff.

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. So basically for application/json content-type azure itself fulfill req.body object with proper request data as json object. For application/x-www-form-urlencoded content-type data comes in raw format, so I used query-string library to parse incoming data and fulfill req.body with json object.

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 application/octet-stream and parse it by yourself, maybe using some existing libraries. But I’m not sure, you better try yourself, I may be wrong.

0reactions
dcolliencommented, Mar 7, 2019

@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:

  "dependencies": {
    "azure-function-express": "github:dcollien/azure-function-express#dist"
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Fix Invalid Content-Type Text HTML - Sitechecker
Learn how to fix invalid Content-Type text HTML errors. We'll show you how to fix the URL and linked data in your web...
Read more >
Setting 'Content-Type' header does not work. #2544 - GitHub
It seems like setting Content-Type header is impossible without data property: const api = Axios.create({ baseURL: 'https://my-url', ...
Read more >
Content-type not working in PHP - Stack Overflow
I have some issues with a PHP file that is not working properly. The Content-type does not get recieved by any browser at...
Read more >
Add a content type to a list or library - Microsoft Support
Under Content Types, select Add from existing site content types. If Content Types doesn't appear, select Advanced settings, and select Yes under Allow...
Read more >
Exposed filter doesn't work properly with content type - Drupal
Exposed filter doesn't work with content type, Show all content types even not selected once. And also wrong default value instead of All...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found