Http Requests with PB server

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Posts: 12
Joined: Fri Sep 25, 2020 1:35 pm

Http Requests with PB server

Post by Joubarbe »

Hi,

PB:

Code: Select all

Procedure SendResponse(clientID.i, response.s, status.s, keep_alive.b = #False)
  If keep_alive
    connection.s = "keep-alive"
  Else
    connection.s = "close"
  EndIf
  template.s = ~"HTTP/1.1 " + status + 
               ~"\r\nAccess-Control-Allow-Origin: *" + ; Private APIs should not allow all origins, but only its domain.
               ~"\r\nConnection: " + connection + 
               ~"\r\nContent-Length: " + StringByteLength(response, #PB_UTF8) + 
               ~"\r\nContent-Type: text/html; charset=utf-8" +
               ~"\r\nServer: PureBasic" +
               ~"\r\nDate: " + FormatDate("%dd/%mm/%yyyy", Date()) + 
               ~"\r\n\r\n" + response
  
  ; See https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html.
  
  SendNetworkString(clientID, template)
EndProcedure
SB:

Code: Select all

Procedure HttpGet(success, result.s, userData)
  If success
    Debug result ; SendNetworkString() on server.
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

HTTPRequest(#PB_HTTP_Get, "http://localhost:6832/get", "name1=value1&name2=value2", @HttpGet())
HTTPRequest(#PB_HTTP_Post, "http://localhost:6832/post", "PostParam", @HttpGet())
HTTPRequest(#PB_HTTP_Put, "http://localhost:6832/put", "PutParam", @HttpGet())
HTTPRequest(#PB_HTTP_Delete, "http://localhost:6832/delete", "DelParam", @HttpGet())
GET and POST work as intended: it is possible to receive the method and the parameters, and send a server response. However:

1. PUT and DELETE both return an error (Debug "HTTPRequest(): Error").
2. Both have an OPTIONS method in the header. I don’t understand why the header does not respect the same standards as GET (GET /get) and POST (POST /post) methods.
3. The server does not receive the parameter. ie. it’s not in the header.

Header received with the PUT method:

Code: Select all

OPTIONS /put HTTP/1.1
Host: localhost:6832
Connection: keep-alive
Accept: */*
Access-Control-Request-Method: PUT
Origin: http://127.0.0.1:9080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
Sec-Fetch-Dest: empty
Referer: http://127.0.0.1:9080/
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,en-GB;q=0.6
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Http Requests with PB server

Post by Peter »

Please open the developer console of your browser (usually with <F12>).
There you will find a more detailed error message.
Joubarbe
Posts: 12
Joined: Fri Sep 25, 2020 1:35 pm

Re: Http Requests with PB server

Post by Joubarbe »

Oops, yeah sorry, this one is a good old "Access to XMLHttpRequest at 'http://localhost:6832/delete' from origin 'http://127.0.0.1:9080' has been blocked by CORS policy: Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response."

Didn’t know it only affects DELETE and PUT. Don’t really know how to fix that right now with SB, as the Headers() parameters of HttpRequest() doesn’t seem to work.
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Http Requests with PB server

Post by Peter »

untested, but try:

Code: Select all

  template.s = ~"HTTP/1.1 " + status +
               ~"\r\nAccess-Control-Allow-Origin: *" + ; Private APIs should not allow all origins, but only its domain.
               ~"\r\nAccess-Control-Allow-Methods: *" + ; <- this
[...]
Joubarbe
Posts: 12
Joined: Fri Sep 25, 2020 1:35 pm

Re: Http Requests with PB server

Post by Joubarbe »

Working now. That should solve all my problems. Thanks!

EDIT: Why do I still have those two OPTIONS headers, besides the proper PUT and DELETE ones? Hmm... :roll: (ie. 6 #PB_NetworkEvent_Data / requests instead of the 4 expected)

EDIT 2: Ok this OPTIONS methods seems to be sent by the browser for pre-approve stuff. Another question while I’m at it: is it normal that I cannot retrieve the error status on SB? 400, 404, etc. I don’t see a way to identify the exact error sent by the server.
Joubarbe
Posts: 12
Joined: Fri Sep 25, 2020 1:35 pm

Re: Http Requests with PB server

Post by Joubarbe »

Again, how do you get the status code sent by the server?
On thick clients (using PB), you can do the following:

Code: Select all

request = HTTPRequest(#PB_HTTP_Get, "http://localhost:6832/get", "name1=value1&name2=value2")
Debug HTTPInfo(request, #PB_HTTP_StatusCode) ; Outputs 200.
Post Reply