【问题标题】:How to post JSON object to server using casper.open()?如何使用 casper.open() 将 JSON 对象发布到服务器?
【发布时间】:2015-02-17 10:37:54
【问题描述】:

我正在尝试使用以下命令向服务器发布请求

this.open('someurl',{
            method: 'post',
            data:   {
                'memberId': obj.id,
                'password': obj.password
            },
            headers: {
                'Accept':'application/json',
                'Content-Type':'application/json',
            }
        }).then(function(response){
            require('utils').dump(response);
        });

但是响应失败

{
"contentType": null,
"headers": [
    {
        "name": "Content-Length",
        "value": "8008"
    },
    {
        "name": "Server",
        "value": "Jetty(8.1.8.v20121106)"
    }
],
"id": 2,
"redirectURL": null,
"stage": "end",
"status": 415,
"statusText": "Unsupported Media Type",
"time": "2015-02-17T10:34:16.952Z",
"url": <someURL>,
"data": null

而等效的 CURL 命令是成功的

curl -i someurl -X POST -H Content-Type:application/json --data '{"memberId": 1234567,"password":"password"}'。

我哪里错了?

【问题讨论】:

    标签: json curl phantomjs httprequest casperjs


    【解决方案1】:

    如果您在机器上使用 npm 或 brew 安装。不起作用。您需要从源代码编译 casperjs 才能使其工作。我没有参考资料,但我也从一个论坛上读到了这篇文章。

    【讨论】:

    • 谢谢@hasan。这解决了我的问题。我在 casperJS 网站github.com/n1k0/casperjs/issues/196 上找到了这个帖子,这表明了同样的事情。但是,没有任何解释
    • 得到了这个神秘行为的解释。最新的 casperjs 版本是在 2013 年 11 月 23 日。这可能是我们通过包管理器下载时会得到的版本。但是,直到 2014 年 2 月 20 日才支持发布 JSON 对象。
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多