mIRC Homepage
Posted By: judge2020 making a post request to a server - 24/08/14 08:19 PM
very bad (if i'm anything at all) with making http requests, just want to make a post request to a server.
can someone create a quick alias and signal for this?

server to post to:
Code:
https://toontowninfinite.com/api/login/

the post request:
Code:
n=ircCheckr&p=judge2020&dist=test

the request header:
Code:
POST /api/login/ HTTP/1.1
Host: toontowninfinite.com
Accept: */*
Content-Length: 33
Content-Type: application/x-www-form-urlencoded

the response should be something like this:
Code:
{"success":false,blablablabla

and it always sends a 200 OK even if success is false. is there a way i can check for the success being false or true instead of checking for 200 OK?
Posted By: Wims Re: making a post request to a server - 24/08/14 08:49 PM
Check http://en.wikichip.org/wiki/mirc/sockets/tcp for more informations
Code:
alias toon {
 if (!$sslready) return $input(SSL is required,o)
 sockclose toon
 sockopen -e toon toontowninfinite.com 443
}
on *:sockopen:toon:{
 if ($sockerr) return $input(Error while trying to connect to the server,o)
 var %sw sockwrite -n toon
 %sw POST /api/login/
 %sw Host: toontowninfinite.com
 %sw Content-Length: 34
 %sw
 sockwrite toon n=ircCheckr&p=judge2020&dist=test
}
on *:sockread:toon:{
  if ($sockerr) return $input(An error occured while reading data,o)
  var %a
  sockread -f %a
  if (*{"success":* iswm %a) {
     echo -a success : $gettok($gettok(%a,2,58),1,44)
  }
}
Posted By: judge2020 Re: making a post request to a server - 24/08/14 09:05 PM
Originally Posted By: helpfile
/sockopen [-de] [bindip] <name> <address> <port>

reading help files and that page, it requires a name. change it to
Code:
  sockopen -e toon toontowninfinite.com 443
but now it's giving no input...
Posted By: Wims Re: making a post request to a server - 24/08/14 09:08 PM
I edited the code right after posting, you must have been lurking the thread..
Posted By: judge2020 Re: making a post request to a server - 24/08/14 09:10 PM
ya, now it works. thanks for the help, going to hook it up to a 10 second timer to check if the status changes smile
Posted By: judge2020 Re: making a post request to a server - 24/08/14 10:02 PM
i keep getting false and error invalid distribution, on requestmaker.com the same n=ircCheckr&p=judge2020&dist=test works so i know it's not that
Posted By: judge2020 Re: making a post request to a server - 24/08/14 10:19 PM
could it be easier if you did hadd %sw?
Posted By: Wims Re: making a post request to a server - 24/08/14 10:46 PM
I also got that error when trying it.
I don't know what to tell you, I just wrote what you asked for, the server is answering (you said it would answer 200 OK, but it does not answer 200 OK at all, it sends some lines full of $chr(32) and then send the JSON data.)

It wouldn't be easier to use an hash table here, unsure what you have in mind, I used a variable to avoid repeating the "sockwrite -n $sockname" stuff, makes the request clearer
Posted By: judge2020 Re: making a post request to a server - 25/08/14 12:23 AM
well when i use a request maker i get HTTP/1.1 200 OK so...
Posted By: Wims Re: making a post request to a server - 25/08/14 01:05 AM
Eh, seeing "HTTP/1.1" in your post helped, that's actually my bad, I forgot to put the HTTP version to use in the POST line, I also counted 34 bytes for the length, but you were right with 33, that didn't fix the problem though, so I added the header you specified in your first post and it worked wink

Update the on sockopen event:
Code:
on *:sockopen:toon:{
  if ($sockerr) return $input(Error while trying to connect to the server,o)
  var %sw sockwrite -n toon
  %sw POST /api/login/ HTTP/1.1
  %sw Host: toontowninfinite.com
  %sw Accept: */*
  %sw Content-Type: application/x-www-form-urlencoded
  %sw Content-Length: 33
  %sw
  sockwrite toon n=ircCheckr&p=judge2020&dist=test
}
Posted By: judge2020 Re: making a post request to a server - 30/08/14 04:30 AM
that worked, thanks :P
Posted By: judge2020 Re: making a post request to a server - 18/10/14 01:13 PM
so coming back to this, i am now using an API to check the status. it was easy altering the toon socket to work with this, but i have a problem.

the $gettok in the sockread event does not work the same as the old one. the old was
Code:
{"success":true/false...
but now it's
Code:
{"public":"1"}
so the current gettok is getting "1"} instead of a just 1 or just 0

thanks for any help
EDIT: nevermind, found $gettok(%a,4,34) to work
© mIRC Discussion Forums