英文:
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 "username=myusername&password=mypassword" -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: '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); }
} );
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.
答案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";
> }
> }
> });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论