发送后请求多部分表单数据。来自某些Microsoft服务的错误“超出了行长度限制100”

时间:2019-06-13 11:10:07

标签: c# asp.net node.js http axios

此数据是从邮递员发送的,它的工作原理是:

这是一个邮递员请求,状态为200:

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

这是我通过Axios从NodeJs发出的请求:

    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

我从ASP服务器收到的错误:Line length limit 100 exceeded

我的请求中缺少什么?

2 个答案:

答案 0 :(得分:6)

根据以下两个github问题:

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

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

此问题是由于无法使用正确的行尾引起的。我无法从您的代码准确地告诉问题发生的位置,但是调试起来应该很简单。

您需要使用代理-我发现Fiddler很好。捕获来自邮递员和您的客户的请求并进行比较。您可能需要将整个请求放到Notepad ++之类的编辑器中,才能查看非打印字符。

一旦发现问题,就可以进行适当的修改以添加或删除\r

答案 1 :(得分:2)

下面的代码在localhost:3000处设置了HTTP服务器,并且对于所有传入请求,服务器将转储原始请求正文。

尝试将您的请求从Postman和Nodejs发送到localhost:3000,并进行比较。

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);

这是示例输出

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

希望这将帮助您调试问题。