This topic was marked solved by its starter, on September 1st, 2020, 02:32 PM
Socket Bad Request

KrOoB_

  • Full Member
  • Keeper
  • Posts: 175
Socket Bad Request
«  »Last edited
links are working also no problem in index but it's getting error while requesting why?


|-> VC:MP Player Since 2014
|-> VC-TR Founder
|-> VC-TR Scripter and Hoster


Xmair

  • Wiki Contributor
  • Posts: 954

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

KrOoB_

  • Full Member
  • Keeper
  • Posts: 175
Re: Socket Bad Request
« Reply #3,  »

i changed it like this but this time it says nothing

|-> VC:MP Player Since 2014
|-> VC-TR Founder
|-> VC-TR Scripter and Hoster


Xmair

  • Wiki Contributor
  • Posts: 954
Re: Socket Bad Request
« Reply #4,  »Last edited
Try using the HTTP Requests plugin by Luckshya.

Code: [Select]
local request = SqHTTP.GetRequest();
request.setURL("www.example.com");
request.setTag("myRequest");
request.sendGet();

function HTTP_OnResponse(tag, url, statusCode, response) {
switch(tag) {
case "myRequest": {
if (statusCode == 200) { // OK
print(response);
}
else { // Error
print(statusCode);
}
}
break;
}
}

Or if you want to stick to the official sockets plugin:
Code: [Select]
local sock = null;
local sockAddress = "www.example.com";

function setupSocket() {
sock = NewSocket("onSocketDataReceive");
sock.SetNewConnFunc("onSocketConnect");
sock.Connect(sockAddress, 80);
print(format("Connecting to %s...", sockAddress));
}

function onSocketDataReceive(data) {
print("Received data:");
print(data);
}

function onSocketConnect() {
print("Connection successful, sending data");

sock.Send("GET / HTTP/1.1\r\n");
sock.Send("Accept: */*\r\n");
sock.Send(format("Host: %s\r\n", sockAddress));
sock.Send("\r\n");
}

setupSocket();

Basically what you were missing was a "\r\n" in the end.

Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

habi

  • Sr. Member
  • Posts: 288
Re: Socket Bad Request
« Reply #5,  »Last edited
hi i was also working on this parallelly and finally got the answer.

Code: [Select]
host<-"vc-mp.org";
request<-"GET / HTTP/1.1\r\nHost: vc-mp.org\r\nAccept: */*\r\n\r\n";
function HTTP()
{
Socket<-NewSocket("abcd");
Socket.SetNewConnFunc("connF");
Socket.Connect(host,80);
}
function abcd(data)
{
print(data);
}
function connF(){
print("connF called");
print(request);
local q=Socket.Send(request);
}

And output

and


Notes:
1. The inbuilt print function is enough.
Code: [Select]
Socket<-NewSocket("print");
2. This \r\n are very important. ( Using @"" is a bit confused. )

KrOoB_

  • Full Member
  • Keeper
  • Posts: 175
Re: Socket Bad Request
« Reply #6,  »Last edited
Quote from habi on September 1st, 2020, 03:24 PM
hi i was also working on this parallelly and finally got the answer.

Code: [Select]
host<-"vc-mp.org";
request<-"GET / HTTP/1.1\r\nHost: vc-mp.org\r\nAccept: */*\r\n\r\n";
function HTTP()
{
Socket<-NewSocket("abcd");
Socket.SetNewConnFunc("connF");
Socket.Connect(host,80);
}
function abcd(data)
{
print(data);
}
function connF(){
print("connF called");
print(request);
local q=Socket.Send(request);
}

And output

and


Notes:
1. The inbuilt print function is enough.
Code: [Select]
Socket<-NewSocket("print");
2. This \r\n are very important. ( Using @"" is a bit confused. )
i just found a json plugin and i'll use it ( i recommend it )

|-> VC:MP Player Since 2014
|-> VC-TR Founder
|-> VC-TR Scripter and Hoster