2

I have an API that returns content with content-type: "multipart/form-data; charset=utf-8". However, in my nodejs app, when I make the following call through superagent:

request
    .get(ApiUrl + id)
    .set('Authorization', basicHttpAuth)
    .set('client_id', clientId)
    .set('client_secret', clientSecret)
    .end(function (err, res) {
        if (err) {
            callback(null, err)
            console.log(err);
        }
        else {
            callback(null, res);
        }
    })

I get this error:

Error: bad content-type header, no multipart boundary

Any idea what's wrong?

Stack Trace:

Error: bad content-type header, no multipart boundary
    at IncomingForm._parseContentType (/Users/mike/Svr/Server/node_modules/formidable/lib/incoming_form.js:271:19)
    at IncomingForm.writeHeaders (/Users/mike/Svr/Server/node_modules/formidable/lib/incoming_form.js:142:8)
    at IncomingForm.parse (/Users/mike/Svr/Server/node_modules/formidable/lib/incoming_form.js:110:8)
    at ClientRequest.<anonymous> (/Users/mike/Svr/Server/node_modules/superagent/lib/node/index.js:869:9)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:564:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
    at TLSSocket.socketOnData (_http_client.js:453:20) response: undefined }

Here is the server response header:

Access-Control-Allow-Origin →*
Connection →keep-alive
Content-Length →44691
Content-Type →multipart/form-data; charset=utf-8
Date →Wed, 05 Jul 2017 03:44:23 GMT

And the body is big blob of text / string.

HelmBurger
  • 1,168
  • 5
  • 15
  • 35
  • I doubt you'd be allowed to use `--` as suffix for boundaries. – robertklep Jul 05 '17 at 07:27
  • Ok but removing that didn't help – HelmBurger Jul 05 '17 at 08:24
  • Setting `multipart/form-data` for a `GET` request doesn't really make sense anyway. Are you sure it's not supposed to be a `POST` request? Also, it doesn't look like you're actually uploading data, so perhaps the `Content-Type` header isn't even necessary at all. – robertklep Jul 05 '17 at 08:44
  • Ok but removing even that doesn't work. The error remains the same. And yes I am not uploading any data, but what I am receiving has Content type "multipart/form-data" – HelmBurger Jul 05 '17 at 08:57
  • The question is where the error is originating from. I initially thought that it was the server that was emitting that error, but now I understand it's the client? I don't expect `superagent` to parse `multipart/form-data` responses, so where's it coming from? Can you post the stack trace that belongs to the error? – robertklep Jul 05 '17 at 09:01
  • Added stack trace – HelmBurger Jul 05 '17 at 09:04
  • Thanks. It looks like `superagent` _does_ parse `multipart/form-data` responses, but that the server response is invalid. Without knowing what the actual server response is, that's going to be difficult to debug. Perhaps you can use `request` instead of `superagent`, or use a tool like cURL to determine what the actual server response is. – robertklep Jul 05 '17 at 09:09
  • just remove `content-type` – Hekmat Jan 26 '22 at 20:34

2 Answers2

0

According to RFC2045:

...while the "boundary" parameter is required for any subtype of the "multipart" media type.

In your case, the server doesn't set that parameter, so the response is invalid and superagent (which tries to parse the response) throws an error.

You may be able to use an alternative HTTP client, provided that it doesn't try to parse multipart/form-data responses.

You could use the built-in http(s).get(), or perhaps the request package.

EDIT: if you're stuck with superagent, then you may be able to monkeypatch it so it will detect the broken response and treat it as text/plain.

Add the following somewhere "high up" in your code (before using superagent):

const Utils     = require('superagent/lib/utils');
const UtilsType = Utils.type;
Utils.type = function(type) {
  if (type === 'multipart/form-data; charset=utf-8') {
    type = 'text/plain; charset=utf-8';
  }
  return UtilsType.call(this, type);
};
robertklep
  • 198,204
  • 35
  • 394
  • 381
0

Normal while passing the multipart-formdata with files you need to set the boundry just incase if the file is of more size then the request made.

  Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL

link

renish p.r
  • 105
  • 1
  • 5