像 curl 一样发送 post 数据

Send post data like curl

我使用这个 curl 命令将文件推送到网络服务器

curl -i -X POST -H 'Content-Type: application/json' -d @./mylocalfile -k https://192.168.1.10:8283/here

我想使用一个简单的网页来代替 select mylocalfile 并且我有这个

<html>
<body>
    <input class="file" type="file" id="fafafa" name="fileupload" /><br/>
    <br/>
    <input type="button" value="Submit" onclick="xhrSubmit();" />
    <script type="text/javascript ">
        function xhrSubmit() {
            var file_obj = document.getElementById('fafafa').files[0];
            var fd = new FormData();
            fd.append('fafafa', file_obj);
            xhr = new XMLHttpRequest();
            xhr.open('POST', 'https://192.168.1.10:8283/here', true)
            xhr.setRequestHeader('Content-type', 'application/json');
            xhr.send(fd);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    var obj = JSON.parse(xhr.responseText);
                    console.log(obj);
                }
            };
        }
    </script>
</body>
</html>

但是当我使用网页时,网络服务器得到的响应是它期望的对象或数组。

如何更改 javascript 代码以像 curl 命令一样发送数据,将文件发布到网络服务器没有问题?

我服务器端的完整提升异常是

Exception <unspecified file>(1): expected object or array

我会说,检查你收到的卷曲版本。不能是application/json和文件数据。你需要 multipart/form-data.

您的内容类型有误。您没有将 JSON 发送到您的服务器。

尝试将内容类型设置为 application/x-www-form-urlencoded

最后的问题是 json 有效载荷消息中有很多换行符,curl 不关心但浏览器关心。