【问题标题】:Why does my http request body doesn't get transferred to the server?为什么我的 http 请求正文没有传输到服务器?
【发布时间】:2020-11-06 16:33:50
【问题描述】:

我已经发出了一个 ajax http post 请求并在 Fiddler 中尝试过,它可以工作,但是当我尝试在 Dart 中运行完全相同的请求时,请求正文没有被传输到服务器。我的 Dart 请求正文有问题吗?

Response response = await client.post(
      'https://intranet.tam.ch/krm/timetable/ajax-get-timetable',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Language': 'de-ch',
        'Accept-Encoding': 'gzip, deflate, br',
        'Host': 'intranet.tam.ch',
        'Origin': 'https://intranet.tam.ch',
        'User-Agent':
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15',
        'Connection': 'keep-alive',
        'Referer': 'https://intranet.tam.ch/krm/calendar',
        'Content-Length': '83',
        'Cookie':
            'school=krm; sturmsession=xx; sturmuser=xx; username=xx',
        'X-Requested-With': 'XMLHttpRequest'
      },
      body:
          'startDate=1597615200000&endDate=598133600000&studentId%5B%5D=x&holidaysOnly=0');


client.close();

print(response.body);

非常感谢任何答案

【问题讨论】:

  • 您的方法的其余部分看起来如何?您是否尝试打印response.body 以查看服务器是否给您某种响应?
  • 我打印了response.body,它说POST参数“startDate”丢失了,即使它存在于体内也是如此。
  • 尝试从您的标题中删除 'Content-Length': '83',。 (它会由库自动再次添加,但如果您的标题中已经有“Content-Length”,它似乎会产生一些问题)。

标签: http post dart xmlhttprequest httpclient


【解决方案1】:

如果您在标头中手动定义Content-Length,则似乎会出现某种奇怪的行为,而正文永远不会被发送。如果您删除标头并让库处理 Content-Length 它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多