Difference between curl request and Grease/TamperMonkey GM_xmlHttpRequest

huangapple go评论72阅读模式
英文:

Difference between curl request and Grease/TamperMonkey GM_xmlHttpRequest

问题

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

我已经成功地使用curl与pyload API通信:

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

这会返回我所需的会话ID,以便继续使用API。

然而,当我尝试从Tampermonkey中使用GM_xmlhttpRequest进行相同的调用时,我总是得到一个带有responseText 'false'的成功响应,这意味着身份验证未成功:

GM_xmlhttpRequest ( {
  context: { contextData: 'foo', contextData2: 'bar' }, // <- 忽略这个,仅用于测试
  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日志中没有看到任何内容。:-)

英文:

I am trying to inject buttons in a website client-sided for easily sending URLs to my pyload instance.
I already did a similar thing to create packages in a local jDownloader instance, so i am not too far off here.

I already managed to successfully talk to the pyload API with curl:

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

which returns me - as it should - a session-id which i need to continue using the api.

However, when i try to make the same call from within Tampermonkey with GM_xmlhttpRequest i always get a success with responseText 'false' - which means the authentication was not successful:

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

} );

My question is:
what am i doing wrong, where is the difference (for the server / pyload) between using curl and using GM_xmlhttpRequest? I thought it should result in basically the same query ?

And no, sadly i do not see anything in the pyload-logs. Difference between curl request and Grease/TamperMonkey GM_xmlHttpRequest

答案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";
    }
  }
});
英文:

When using POST method in GM.xmlHttpRequest/GM_xmlhttpRequest, you need to set Content-Type header as well.

> POST request
>
> When making a POST request, most sites require the Content-Type header
> to be defined as such:
>
> 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";
> }
> }
> });

huangapple
  • 本文由 发表于 2020年1月7日 00:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定