简体   繁体   中英

Status-415 Invalid content-type specified error in nodejs

I would like to know how to send form data with headers to request module.

I have function getToken which makes post request with headers and formdata, and then response data will again make a request to new url and return data.

Currenlty returning { type: 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', title: 'Unsupported Media Type', status: 415, detail: 'Invalid content-type specified' } .

Apologies since not able to provide the apiurl and key


//app.js
var express = require('express');
var router = express.Router();
var helper= require('../help.js');
router.get('/', function (req, res) {
    helper.getToken(req.originalUrl).then(token=>{                        
      helper.getData(token).then(data=>{
         res.send({api:data})
      })
   })
})
//helper.js
module.exports.getToken= function (rquery) {
  return new Promise(async function (resolve, reject) {
    try {
      const form_data = {
        grant_type: 'auth_token',
        auth_token: ''//apikey,
         }
      var headers = {
       "Content-Type": "multipart/form-data",
      };
      url = ``//apiurl1;
      request.post({ url: url, form: form_data, headers: headers },  (e, r, body)  => {
        if (!e) {
         resolve(JSON.parse(body)); 
        }
        else {
          resolve(e);
        }
      });
    }
    catch (err) {
      reject(err);
    }
  })
}
module.exports.getData= function (token) {
  return new Promise(async function (resolve, reject) {
    try {
        const form_data = {
        grant_type: 'refresh_token',
        refresh_token: token.refresh_token,
      }
      var headers = {
        "Content-Type": "multipart/form-data",
        "Authorization":"Bearer "+token.access_token
      };
      url = ``//apiurl2;
      request.post({ url: url, form: form_data, headers: headers },  (e, r, body)  => {
        if (!e) {
         console.log(JSON.parse(body));// returns errors 415
         resolve(JSON.parse(body));
        }
        else {
          resolve(e);
        }
      });
    }
    catch (err) {
      reject(err);
    }
  })
}

It seems that the API you are posting to does not support multipart/form-data. Can you confirm the Content-Type on the API?

EDIT: Found this in npmjs page of request module: https://www.npmjs.com/package/request#multipartform-data-multipart-form-uploads

The key that you are sending is "form". Try sending "formData" instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM