Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending post request multipart form data. Error from some microsoft service "Line length limit 100 exceeded"

This data is sent from Postman and it works:

This is a postman request which passes with a 200 status:

POST /api/upload HTTP/1.1

Host: api.test.contoso.se

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

Authorization: Basic 123

User-Agent: PostmanRuntime/7.13.0

Accept: */*

Cache-Control: no-cache

Postman-Token: 089af753-fa12-46c4-326f-dfc39c36faab,c5977145-ece3-4b53-93ff-057788eb0dcf

Host: api.test.contoso.se

accept-encoding: gzip, deflate

content-length: 18354

Connection: keep-alive

cache-control: no-cache

Content-Disposition: form-data; name="Lang"

SV
------WebKitFormBoundary7MA4YWxkTrZu0gW--

Content-Disposition: form-data; name="File"; filename="/C:/Users/file.docx


------WebKitFormBoundary7MA4YWxkTrZu0gW--

Content-Disposition: form-data; name="Login"

ABC

This is my request from NodeJs via Axios:

    const form_data = new FormData();
        form_data.append("File", fs.createReadStream(pathToFile));
        form_data.append('Login', alias.toUpperCase());
        console.log(form_data); // se output down
        const request_config = {
            headers: {
                "Authorization": "Basic 123",
                "Content-Type": `multipart/form-data; boundary=${form_data._boundary}`
            },
            data: form_data
        };

console.log(form_data):

FormData {

  _overheadLength: 540,

  _valueLength: 13,

  _valuesToMeasure:

   [ ReadStream {

       _readableState: [ReadableState],

       readable: true,

       _events: [Object],

       _eventsCount: 3,

       _maxListeners: undefined,

       path:

        '/Users/qq/test.docx',

       fd: null,

       flags: 'r',

       mode: 438,

       start: undefined,

       end: Infinity,

       autoClose: true,

       pos: undefined,

       bytesRead: 0,

       closed: false,

       emit: [Function] } ],

  writable: false,

  readable: true,

  dataSize: 0,

  maxDataSize: 2097152,

  pauseStreams: true,

  _released: false,

  _streams:

   [ '----------------------------610001147909085905792533\r\nContent-Disposition: form-data; name="File"; filename="test.docx"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n',

     DelayedStream {

       source: [ReadStream],

       dataSize: 0,

       maxDataSize: Infinity,

       pauseStream: true,

       _maxDataSizeExceeded: false,

       _released: false,

       _bufferedEvents: [Array],

       _events: [Object],

       _eventsCount: 1 },

     [Function: bound ],

     '----------------------------610001147909085905792533\r\nContent-Disposition: form-data; name="Login"\r\n\r\n',

     'abc',

     [Function: bound ] ],

  _currentStream: null,

  _insideLoop: false,

  _pendingNext: false,

  _boundary: '--------------------------610001147909085905792533

The error I get from ASP server: Line length limit 100 exceeded

What am I missing in my request?

like image 703
Joe Avatar asked Jun 13 '19 11:06

Joe


2 Answers

According to these two github issues:

https://github.com/aspnet/AspNetCore/issues/2939

https://github.com/aspnet/AspNetCore/issues/3724

The issue is caused by a failure to use the correct line endings. I can't tell from your code exactly where the issue is occuring, but it should be fairly straightforward to debug it.

You need to use a proxy - I find Fiddler to be very good. Capture the request from Postman and from your Client and compare them. You may need to drop the whole request into an editor like Notepad++ to be able to view the non-printing characters.

Once you find the issue, it should be straightforward to amend to add or remove \r as appropriate.

like image 161
ste-fu Avatar answered Nov 14 '22 23:11

ste-fu


The code below setup a HTTP server at localhost:3000, and for all incoming requests, the server dumps the raw request body.

Try posting your request to localhost:3000 from both Postman and Nodejs, and compare the difference.

require('http').createServer((req, res) => {
    req.on("data", _ => _)
       .on("end" , _ => res.end(req.socket.rawBody));
}).on('connection', socket => {
    socket.rawBody = "";
    socket.on('data', data => socket.rawBody += data.toString());
}).listen(3000);

This is the sample output

POST / HTTP/1.1
Authorization: Basic QUJDOkFCQw==
User-Agent: PostmanRuntime/7.15.0
Accept: */*
Cache-Control: no-cache
Host: localhost:3000
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------540608501697240762060297
content-length: 268
Connection: keep-alive

----------------------------540608501697240762060297
Content-Disposition: form-data; name="Lang"

SV
----------------------------540608501697240762060297
Content-Disposition: form-data; name="Login"

ABC
----------------------------540608501697240762060297--

Hope this will help you debug the problem.

like image 27
Neverever Avatar answered Nov 14 '22 21:11

Neverever