【问题标题】:Difference between curl request and Grease/TamperMonkey GM_xmlHttpRequestcurl 请求和 Grease/TamperMonkey GM_xmlHttpRequest 之间的区别
【发布时间】:2020-04-24 04:35:38
【问题描述】:

我正在尝试在网站客户端中注入按钮,以便轻松地将 URL 发送到我的 pyload 实例。 我已经做了类似的事情来在本地 jDownloader 实例中创建包,所以我在这里并不太远。

我已经成功地使用 curl 与 pyload API 对话:

curl -s -d "username=myusername&password=mypassword" -X POST http://MYPYLOADINSTANCE:8000/api/login

它返回给我 - 它应该 - 我需要继续使用 api 的会话 ID。

但是,当我尝试在 Tampermonkey 中使用 GM_xmlhttpRequest 进行相同的调用时,我总是会通过 responseText 'false' 获得成功 - 这意味着身份验证不成功:

GM_xmlhttpRequest ( {
  context: { contextData: 'foo', contextData2: 'bar' }, // <- ignore that, only for testing
  method:  'POST',
  data: 'username=myusername&password=mypassword',
  synchronous: false,
  url:     'http://MYPYLOADINSTANCE:8000/api/login',
  onload:  function(responseDetails) { alert(responseDetails.responseText
          + '\n' + responseDetails.context.contextData); },
  onerror: function(responseDetails) { alert(responseDetails); },
  onabort: function(responseDetails) { alert(responseDetails); }

});

我的问题是: 我做错了什么,使用 curl 和使用 GM_xmlhttpRequest 之间的区别(对于服务器/pyload)在哪里?我认为它应该产生基本相同的查询?

不,遗憾的是我在 pyload-logs 中没有看到任何内容。 :-(

【问题讨论】:

    标签: curl xmlhttprequest greasemonkey tampermonkey gm-xmlhttprequest


    【解决方案1】:

    GM.xmlHttpRequest/GM_xmlhttpRequest 中使用 POST 方法时,还需要设置 Content-Type 标头。

    POST 请求

    发出 POST 请求时,大多数网站都需要 Content-Type 标头 要这样定义:

    GM.xmlHttpRequest({
      method: "POST",
      url: "http://www.example.net/login",
      data: "username=johndoe&password=xyz123",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      onload: function(response) {
        if (response.responseText.indexOf("Logged in as") > -1) {
          location.href = "http://www.example.net/dashboard";
        }
      }
    });
    

    【讨论】:

    • 谢谢!这正是解决方案。现在我觉得很傻。
    猜你喜欢
    • 2020-07-06
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2010-10-26
    • 2021-05-28
    • 2016-04-22
    相关资源
    最近更新 更多