首页 > 解决方案 > ASP Classic 解析来自 curl POST -F 的数据

问题描述

我有以下指向我的服务的 CURL 请求:

curl -X POST \
  http://go.example.com/ \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Postman-Token: cf0c1ab5-08ff-1aa2-428e-24b855e1a61c' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F fff=vvvvv \
  -F rrrr=ddddd \
  -F xx=something

我试图在经典的 ASP 代码中捕捉 xx 参数。我试过'Request(“xx”)'和'Request.Form(“xx”)'。

你有什么主意吗?

标签: vbscriptasp-classic

解决方案


这是来自CURL 文档

-F,--表格

(HTTP SMTP IMAP)对于 HTTP 协议族,这让 curl 模拟用户按下提交按钮的填写表单。这会导致 curl根据 RFC 2388使用 Content-Type multipart/form-data发布数据。

当使用内容类型将表单提交给 Classic ASP 时multipart/form-data,唯一可用的方法是Request.BinaryRead()按原样Request.Form处理application/x-www-form-urlencoded数据。

Request.BinaryRead()这是一个让您入门的快速示例:

<%
'Should be less than configured request limit in IIS.
Const maxRequestSizeLimit = ...
Dim dataSize: dataSize = Request.TotalBytes
Dim formData

If dataSize < maxRequestSizeLimit Then
  'Read bytes into a SafeArray
  formData = Request.BinaryRead(dataSize)
  'Once you have a SafeArray its up to you to process it.
  ...
Else
  Response.Status = "413 PAYLOAD TOO LARGE"
  Response.End
End If
%>

解析 SafeArray 并不容易

如果您想继续使用Request.Form,可以通过在 CURL 命令中使用-d而不是指定表单参数来完成-F。从文档中

-d,--数据

(HTTP) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的一样。这将导致 curl 使用 content-type application/x-www-form-urlencoded 将数据传递给服务器。与 -F、--form 进行比较。

所以 CURL 命令会是这样的;

curl -X POST \
  http://go.mytest-service.com/ \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d fff=vvvvv \
  -d rrrr=ddddd \
  -d xx=something

然后,您将使用以下xx方法在 Classic ASP 中检索参数;

<%
Dim xx: xx = Request.Form("xx")
%>

有用的链接


推荐阅读