Help needed

Just starting out? Need help? Post your questions and find answers here.
drahneir
Posts: 11
Joined: Wed Feb 17, 2016 3:40 pm

Help needed

Post by drahneir »

I am a bloody newcomer with respect to web programming, and the first command I tried to use lead, of course, to an error.
The command "HttpRequest() seems to be an unsolveable puzzle for me:
1. the second parameter is URL$, which is for me a string. In the example it is a numeric constant.
2. in the explanation for URL$ appears "...on the same domain". Same domain of what?
3. for sending something to my website, the username and password are required. I guess that they must be in "Parameters", but in which form?
I hope that someone can fill my gaps in understanding.
Regards
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Help needed

Post by SparrowhawkMMU »

No worries, we were all beginners once :)

OK, so to take your points:

HTTPRequest takes these parameters:
1: http verb.
This will be either #PB_HTTP_Get or #PB_HTTP_Post. No other http verb is supported as yet

2. URL:
this will be something like: "http://www.mysite.com" or "http://127.0.0.1/mysite/index.php" or whatever

3. Parameters
Took me a while to figure thhis out too as the Help page is less than helpful on this:

say you have foo with a value of 1 and bar with a value of 2, it would be:
"foo=1&bar=2"

So basically, you need to concatenate key-value pairs using the & ampersand character

This applies to both GET and POST

4. Callback procedure/function. So if you want to handle the results in MyHandler(), this would be @MyHandler()

5. userdata - an integer value - useful if you want to pass an ID or other flag form the calling procedure to the callback

Note that you should probably URLEncoder() the URL and parameter list first (this will convert spaces and other non-url safe characeters into special sequences of characters that are safe to send)

The callback function you specify in point 4 above must have exactly 3 parameters. By convention they are called:

success.i (or just Success) - 0 for failure, 1 for success

result.s (or Result$) the actual results from calling the remote endpoint/page

userData.i (or UserData) - the integer you optionally sent in point 5 above

eg:

Code: Select all

Procedure MyHandler(success.i, result.s, userData.i)
  
  If success = #True
    Debug "Result = " + result
    Debug "User Data = " + Str(userData)
  Else
    Debug "What a shame, the http request failed"
  EndIf
  
EndProcedure

HTTPRequest(#PB_HTTP_Get, "http://www.mysite.com", "foo=1&bar=2", @MyHandler(), 1234)

re Same domain -

In the web world, you cannot make cross-domain requests, for security reasons. So for example you cannot run your SB site on http://www.mysite.com and ask it to get data from http://www.yoursite.com

However, search for CORS on this forum as there is a way around this: basically the site/domain you are calling has to allow the calling site access, using a special origin header. Plenty of examples on these forums

re Username and password - see answer 3 above.



Sorry, I don't have access to SpiderBasic on this machine so I may have made a mistake above - anyone else chip in if I have.
Last edited by SparrowhawkMMU on Sat Feb 20, 2016 8:15 pm, edited 1 time in total.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Help needed

Post by SparrowhawkMMU »

Re CORS, see this thread: http://forums.spiderbasic.com/viewtopic ... cors#p1715

The second post has a link to the header that is required.
drahneir
Posts: 11
Joined: Wed Feb 17, 2016 3:40 pm

Re: Help needed

Post by drahneir »

Thanks, you helped me a lot.
Will see, if I can apply it in practice.
QuimV
Posts: 2
Joined: Wed Apr 29, 2015 8:09 pm

Re: Help needed

Post by QuimV »

HTTPRequest is not working either.

My test scenario:

I execute a MockAServ web server on http port 9980 and then, execute this spiderbasic code (taken from help):
(MockAServ is available at- http://mockaserv.stschnell.de/.)

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData)
  Debug Success
  Debug Result$
  Debug UserData
  
  If Success
    Debug Result$
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure
  
  ; Get the content of this file, and display it in the debug window
  ;
Debug "->"
URL$ = URLEncoder("http://localhost:9980/index.html")
HTTPRequest(#PB_HTTP_Get, URL$, "", @HttpGetEvent())
Debug "<-"
The result of HTTPRequest is always: "HTTPRequest(): Error"

NOTES:

1.- If I try to get the page (http://localhost:9980/index.html) using Chrome, I get the page without any problem.

2.- When I get the page from Chrome, MockAServ server receive this text:

GET /index.html HTTP/1.1
Host: localhost:9980
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: es-ES,es;q=0.8
Cookie: PHPSESSID=kvmevnc5f5akic09mb6udbs910

3.- When I get the page from HTTPRequest, MockAServ server receive this other text:

GET /index.html?_=1456768567380 HTTP/1.1
Host: localhost:9980
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
Accept: text/html, */*; q=0.01
Origin: http://127.0.0.1:9080
Referer: http://127.0.0.1:9080/SpiderBasic_Compilation0.html
Accept-Language: es-ES
Accept-Encoding: gzip, deflate
Connection: keep-alive

And MockAServ server, answers with this text, that do not reach my callback function:

HTTP/1.1 404 Not Found
Content-Length: 46
Content-Type: text/html

The resource you are looking for was not found: index.html

;-------------------
It seems that HTTPRequest asks for resource "/index.html?_=1456768567380 ", that is different to "/index.html", but, anyway, my callback function receives nothing.

Could anybody help me?

Thanks in advanced
Post Reply