Page 1 of 1

Peticion por URL

PostPosted: Mon Aug 10, 2020 10:45 pm
by servicomver
Hola, como puedo mandar peticiones a una URL y obtener respuesta de las mismas ?
por ejemplo
https://192.168.0.2/ServerPos/index.php? operation=doTest&name=admin&password=123
necesito obtener una respuesta de esa petición, la pregunta es:
1.-Como hago la petición en formato JSON
2.-Como obtengo la respuesta

Ya busqué algunos ejemplos pero no he logrado nada, es necesario alguna librería o conector ?
Gracias por su tiempo, saludos.

oHttp:= CreateObject("Microsoft.XMLHTTP")
cUrl = "https://192.168.0.2/ServerPos/index.php?"
oHttp:Open( "POST", cUrl, .F.)
oHttp:setRequestHeader( "Content-type", "text/json" )
cJson :='{"name":"admin","password":"123"}'
oHttp:Send(cJson) ("Aquí sale error de conexión")

Re: Peticion por URL

PostPosted: Tue Aug 11, 2020 8:36 am
by cnavarro
servicomver wrote:Hola, como puedo mandar peticiones a una URL y obtener respuesta de las mismas ?
por ejemplo
https://192.168.0.2/ServerPos/index.php? operation=doTest&name=admin&password=123
necesito obtener una respuesta de esa petición, la pregunta es:
1.-Como hago la petición en formato JSON
2.-Como obtengo la respuesta

Ya busqué algunos ejemplos pero no he logrado nada, es necesario alguna librería o conector ?
Gracias por su tiempo, saludos.

oHttp:= CreateObject("Microsoft.XMLHTTP")
cUrl = "https://192.168.0.2/ServerPos/index.php?"
oHttp:Open( "POST", cUrl, .F.)
oHttp:setRequestHeader( "Content-type", "text/json" )
cJson :='{"name":"admin","password":"123"}'
oHttp:Send(cJson) ("Aquí sale error de conexión")


Lo que te falta es mirar la respuesta obtenida
Code: Select all  Expand view

           local cResponse  := ""
           local cMyRespuesta

.../...
// Tu codigo anterior, y después del Send, esto

           cResponse := ohttp:responseText
            cMyRespuesta  := hash()
            hb_jsondecode( cResponse , @cMyrespuesta )
            XBrowse( cMyRespuesta )         // ? cMyRespuesta

 

Re: Peticion por URL

PostPosted: Wed Aug 12, 2020 4:20 pm
by servicomver
Muchisimas gracias, efectivamente no recogia la respuesta. Otra consulta:

hay alguna forma de usar hb_jsonEncode y hb_jsonDecode en xHarbour ?, que tengo que hacer para usarlas ?

Gracias

Re: Peticion por URL

PostPosted: Wed Aug 12, 2020 9:35 pm
by Enrico Maria Giordano
They are available in xHarbour too.

EMG

Re: Peticion por URL

PostPosted: Fri Oct 09, 2020 2:11 pm
by lorenzoazz
Hi, i am fivewin/xharbour programmer and i am trying to capture the response from an http request ( i need to send an address and obtain the POSTAL ITALIAN CODE).
I tried this
Function http(therequest)
Local oHttp
default therequest:= "https://www.nonsolocap.it/cap?k=calori&b=&c=bologna"



Try
* oHttp := CreateObject( "winhttp.winhttprequest.5.1" )
oHttp:= CreateObject("Microsoft.XMLHTTP")

Catch
MsgInfo("errore connessione msxml2, uso microsoft ")
oHttp := CreateObject( 'Microsoft.XMLHTTP' )
End

oHttp:Open( 'GET', therequest, .F. )
oHttp:SetRequestHeader( "Content-Type","application/json")
oHttp:Send()

Try
cResp := oHttp:ResponseBody
Catch
MsgInfo("errore responsebody")

End
*oHttp:SetRequestHeader( "Content-Type","application/json")


SysRefresh()

MsgInfo(cResp)

Return

But i get the error from oHttp:ResponseBody : cresp dosen't exist

Can anyone help me??