1

我正在尝试在网站客户端中注入按钮,以便轻松地将 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 中没有看到任何内容。:-(

4

1 回答 1

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-01-07T05:38:24.413 回答