Need help in http request node

Hi im trying to figure how to use curl in nodered ..I want to get information of my torrent files (which has api support)
i want to convert this part of code to nodered style.

$ curl -i --header 'Referer: http://localhost:8080' --data 'username=admin&password=adminadmin' http://localhost:8080/api/v2/auth/login
HTTP/1.1 200 OK
Content-Encoding:
Content-Length: 3
Content-Type: text/plain; charset=UTF-8
Set-Cookie: SID=hBc7TxF76ERhvIw0jQQ4LZ7Z1jQUV0tQ; path=/
$ curl http://localhost:8080/api/v2/torrents/info --cookie "SID=hBc7TxF76ERhvIw0jQQ4LZ7Z1jQUV0tQ"

and get the information in debug window.
the api documentation is as follows:

Try this...

[{"id":"606e0c6c93bc3b1a","type":"inject","z":"c876e57260edd2ab","name":"Click me","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":640,"y":780,"wires":[["4a881416445ff3e7"]]},{"id":"4a881416445ff3e7","type":"function","z":"c876e57260edd2ab","name":"","func":"//original curl:  \n//\n//curl -i --header 'Referer: http://localhost:8080' --data 'username=admin&password=adminadmin' http://localhost:8080/api/v2/auth/login\n\nmsg.method = \"post\";\nmsg.url = \"http://localhost:8080/api/v2/auth/login\";\nmsg.payload = `username=admin&password=adminadmin`;\nmsg.headers = {\n    \"Referer\": \"http://localhost:8080\",\n    \"Content-Type\": \"application/x-www-form-urlencoded\"\n};\nmsg.cookies = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":780,"wires":[["3695a2656792ac3a"]]},{"id":"3695a2656792ac3a","type":"http request","z":"c876e57260edd2ab","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1000,"y":780,"wires":[["2e35023de976776e"]]},{"id":"2e35023de976776e","type":"debug","z":"c876e57260edd2ab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1180,"y":780,"wires":[]},{"id":"63543d5f2f46c634","type":"comment","z":"c876e57260edd2ab","name":"Edit this function ↓ and set username/password","info":"","x":860,"y":740,"wires":[]}]

Copy the above flow JSON and import it into node-red using CTRL-I

Set the url/username/password up in the function node before operating.

1 Like

yeah this said ok in debug but how to send this to get info

$ curl http://localhost:8080/api/v2/torrents/info --cookie "SID=hBc7TxF76ERhvIw0jQQ4LZ7Z1jQUV0tQ"

Set the debug to "show complete message" - you will find the cookie.

Use the "copy path" button on the debug output field value (to grab its path)

Use this path to populate the msg.cookies and run the 2nd HTTP-request

inject --> 1st http-request --> grab cookie, set msg.cookies --> 2nd http-request

Something like this in a function node before the 2nd http-request...

msg.cookies = {
    "SID": msg.cookies.SID 
};
return msg;

thanks tried something and it works but not sure its the right way

Problem solved.

If you want to fully understand what is going on then read up on the HTTP spec in particular headers and cookies :slight_smile:

1 Like

thanks but one last help
can you help how to do this ?
reference help( i want to add torrent with magnet)

Search the forum for POST and multipart.

Also, you might want to consider using a dedicated API helper by importing it in a function node (qBittorrent - npm search)

cant we do this the same way we did with http request post method?

POST /api/v2/torrents/add HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1
Cookie: SID=your_sid
Content-Type: multipart/form-data; boundary=---------------------------6688794727912
Content-Length: length

-----------------------------6688794727912
Content-Disposition: form-data; name="urls"

https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E7.torrent
https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E8.torrent
-----------------------------6688794727912
Content-Disposition: form-data; name="savepath"

C:/Users/qBit/Downloads
-----------------------------6688794727912
Content-Disposition: form-data; name="cookie"

ui=28979218048197
-----------------------------6688794727912
Content-Disposition: form-data; name="category"

movies
-----------------------------6688794727912
Content-Disposition: form-data; name="skip_checking"

true
-----------------------------6688794727912
Content-Disposition: form-data; name="paused"

true
-----------------------------6688794727912
Content-Disposition: form-data; name="root_folder"

true
-----------------------------6688794727912--

i just want this part to work
and say i want to add a torrent magnet url magnet:?xt=urn:btih:a751ad1260d3f1a353cddc20783c8e250579aa10&dn=Thor+and+Torchlight.wmv&tr=http%3A%2F%2Ftracker.anirena.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Fopen.nyaatorrents.info%3A6544%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce
i understand how post works but this part im really confused, hoping for your help. sorry to bother @Steve-Mcl

Yes.

1 Like

@Steve-Mcl thanks but cant get it to work.. my code in function node.. what am i doing wrong?

var x = "SID="
var cookie = x + y

msg.url = "http://192.168.0.104:8080/api/v2/torrents/add HTTP/1.1";

msg.headers = {};
msg.method = "POST";
msg.headers["Cookie"] = cookie;
msg.headers = {
    "Content-Type": "multipart/form-data; boundary=---------------------------6688794727912"
}
msg.headers = {
    "Content-Length": "length"
}
msg.headers = {
    "Host": "127.0.0.1"
}
msg.headers = {
    "User-Agent": "Fiddler"
}
msg.cookie = cookie;
msg.payload = '-----------------------------6688794727912\r\n'+
    'Content-Disposition: form-data; name="urls"\r\n' +
    '\r\n' +
    'magnet:?xt=urn:btih:0f45480a69ff7b2e948ccfed5dbb25fbed065c41&dn=YouTube%20Video%20Downloader%20PRO%20FINAL%20v4.9.0.3%20%5bTechTools.NET%5d&tr=udp%3a%2f%2fopen.demonii.com%3a1337%2fannounce&tr=udp%3a%2f%2f9.rarbg.to%3a2710%2fannounce&tr=udp%3a%2f%2fcoppersurfer.tk%3a6969%2fannounce&tr=udp%3a%2f%2f9.rarbg.me%3a2710%2fannounce&tr=udp%3a%2f%2ftracker.istole.it%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&tr=udp%3a%2f%2fglotorrents.pw%3a6969%2fannounce&tr=http%3a%2f%2fexplodie.org%3a6969%2fannounce&tr=http%3a%2f%2ftracker.nwps.ws%3a6969%2fannounce&tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.yify-torrents.com%2fannounce&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&tr=http%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2fopentracker.i2p.rocks%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.internetwarriors.net%3a1337%2fannounce&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.zer0day.to%3a1337%2fannounce\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="savepath"\r\n'
    '\r\n' +
    'C:/Users/qBit/Downloads\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="cookie"\r\n' +
    '\r\n' +
    'ui=28979218048197\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="category"\r\n' +
    '\r\n' +
    'radarr\r\n'
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="skip_checking""\r\n' +
    '\r\n' +
    'true\r\n'
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="root_folder""\r\n' +
    '\r\n' +
    'true\r\n';
return msg;




return msg; 

reference:

POST /api/v2/torrents/add HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1
Cookie: SID=your_sid
Content-Type: multipart/form-data; boundary=---------------------------6688794727912
Content-Length: length

-----------------------------6688794727912
Content-Disposition: form-data; name="urls"

https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E7.torrent
https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E8.torrent
-----------------------------6688794727912
Content-Disposition: form-data; name="savepath"

C:/Users/qBit/Downloads
-----------------------------6688794727912
Content-Disposition: form-data; name="cookie"

ui=28979218048197
-----------------------------6688794727912
Content-Disposition: form-data; name="category"

movies
-----------------------------6688794727912
Content-Disposition: form-data; name="skip_checking"

true
-----------------------------6688794727912
Content-Disposition: form-data; name="paused"

true
-----------------------------6688794727912
Content-Disposition: form-data; name="root_folder"

true
-----------------------------6688794727912--

debug:

{"_msgid":"5534682ae2afb21d","method":"POST","url":"http://192.168.0.104:8080/api/v2/torrents/add","payload":"","headers":{"connection":"close","content-length":"0","date":"Thu, 06 Oct 2022 12:16:08 GMT","x-node-red-request-node":"6aee9102"},"cookies":null,"statusCode":400,"responseUrl":"http://192.168.0.104:8080/api/v2/torrents/add","redirectList":[],"retry":0,"responseCookies":{"SID":{"path":"/","SameSite":"Strict","value":"uMSZDokpbLP/O4yNtWgsF+LPXxLb50Cu"}},"cookie":"SID=uMSZDokpbLP/O4yNtWgsF+LPXxLb50Cu"}

Every time you set msg.headers to a new {object} containing one item (and destroy the previous entry). Do the same as you did for msg.headers["cookie"].

Also, current length needs to be a value, not a string of "length" though you might get away with leaving this particular header out.

thanks let me try, msg.headers[""] or can it work like this?

msg.headers = {
    "Content-Type": "multipart/form-data; boundary=---------------------------6688794727912",
    "Content-Length": "length",
    "Host": "127.0.0.1",
    "User-Agent": "Fiddler",
}

@Steve-Mcl even this isn't working

var y = msg.responseCookies.SID.value
var x = "SID="
var cookie = x + y

msg.url = "http://192.168.0.104:8080/api/v2/torrents/add";

msg.headers = {};
msg.method = "POST";
msg.headers["cookie"] = "cookie";
msg.headers["POST"] = "/api/v2/torrents/add HTTP/1.1";
msg.headers["Content-Type"] = "multipart/form-data; boundary=---------------------------6688794727912";
msg.headers["Host"] = "192.168.0.104:8080";
msg.cookie = cookie;
msg.payload = '-----------------------------6688794727912\r\n'+
    'Content-Disposition: form-data; name="urls"\r\n' +
    '\r\n' +
    'magnet:?xt=urn:btih:0f45480a69ff7b2e948ccfed5dbb25fbed065c41&dn=YouTube%20Video%20Downloader%20PRO%20FINAL%20v4.9.0.3%20%5bTechTools.NET%5d&tr=udp%3a%2f%2fopen.demonii.com%3a1337%2fannounce&tr=udp%3a%2f%2f9.rarbg.to%3a2710%2fannounce&tr=udp%3a%2f%2fcoppersurfer.tk%3a6969%2fannounce&tr=udp%3a%2f%2f9.rarbg.me%3a2710%2fannounce&tr=udp%3a%2f%2ftracker.istole.it%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&tr=udp%3a%2f%2fglotorrents.pw%3a6969%2fannounce&tr=http%3a%2f%2fexplodie.org%3a6969%2fannounce&tr=http%3a%2f%2ftracker.nwps.ws%3a6969%2fannounce&tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.yify-torrents.com%2fannounce&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&tr=http%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2fopentracker.i2p.rocks%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.internetwarriors.net%3a1337%2fannounce&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.zer0day.to%3a1337%2fannounce\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="savepath"\r\n' +
    '\r\n' +
    'C:/Users/qBit/Downloads\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="cookie"\r\n' +
    '\r\n' +
    'ui=28979218048197\r\n'+
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="category"\r\n' +
    '\r\n' +
    'radarr\r\n' +
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="skip_checking""\r\n' +
    '\r\n' +
    'true\r\n' +
    '-----------------------------6688794727912\r\n' +
    'Content-Disposition: form-data; name="root_folder""\r\n' +
    '\r\n' +
    'true\r\n';
return msg;

same debug messsage with error 400
when i try to check with chrome network tool it shows this when a torrent is added manually

@Steve-Mcl quick progress but still does not work says error code 403 forbidden but getting better response

did you provide username/password?

Perhaps if qTorrent lets you, turn off HTTP security till you get it working.

Yes in 1st function node

turned off password requirement for nodered server.
@Steve-Mcl this works with status code 200 but payload fails

nodered server?

what is serving /api/v2/torrents/add?

its working now..

var y = msg.responseCookies.SID.value
var x = "SID="
var cookie = x + y

msg.url = "http://192.168.0.104:8080/api/v2/torrents/add";

msg.headers = {};
msg.method = "POST";
msg.headers["cookie"] = "cookie";
msg.headers["POST"] = "/api/v2/torrents/add HTTP/1.1";
msg.headers["Content-Type"] = "multipart/form-data; boundary=---------------------------6688794727912";
msg.headers["Host"] = "192.168.0.104:8080";
msg.cookie = cookie;
msg.payload = '-----------------------------6688794727912\r\n'+
    'Content-Disposition: form-data; name="urls"\r\n' +
    '\r\n' +
    'magnet:?xt=urn:btih:0f45480a69ff7b2e948ccfed5dbb25fbed065c41&dn=YouTube%20Video%20Downloader%20PRO%20FINAL%20v4.9.0.3%20%5bTechTools.NET%5d&tr=udp%3a%2f%2fopen.demonii.com%3a1337%2fannounce&tr=udp%3a%2f%2f9.rarbg.to%3a2710%2fannounce&tr=udp%3a%2f%2fcoppersurfer.tk%3a6969%2fannounce&tr=udp%3a%2f%2f9.rarbg.me%3a2710%2fannounce&tr=udp%3a%2f%2ftracker.istole.it%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&tr=udp%3a%2f%2fglotorrents.pw%3a6969%2fannounce&tr=http%3a%2f%2fexplodie.org%3a6969%2fannounce&tr=http%3a%2f%2ftracker.nwps.ws%3a6969%2fannounce&tr=udp%3a%2f%2fexodus.desync.com%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.yify-torrents.com%2fannounce&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce&tr=http%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2fopentracker.i2p.rocks%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.internetwarriors.net%3a1337%2fannounce&tr=udp%3a%2f%2ftracker.leechers-paradise.org%3a6969%2fannounce&tr=udp%3a%2f%2ftracker.zer0day.to%3a1337%2fannounce\r\n';
return msg;

image