FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Descargar archivo devuelto por un json (Solucionado)
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Descargar archivo devuelto por un json (Solucionado)
Posted: Sun Oct 22, 2023 01:40 AM
Muchachos:
Mediante fwh como puedo descargar el archivo devuelto por una peticion en json?
Para llegar a descargar el archivo hago estos pasos
1. Genero un token (peticion post)
2. Solicito propuesta (informacion a consultar) termina esta peticion con un numero de ticket
3. Solicito el estado de ticket con el no. de ticket generado en el paso 2, termina con la respuesta del estado del ticket y el nombre del archivo a descargar (archivo zip)
4. Solicito descargar el archivo zip, esto me imprime la pantalla


Hice la prueba en postman y sale esto


Como hago para descargar el archivo mediante fwh?

Pego mi cod. fuente, espero me ayuden, gracias
Code (fw): Select all Collapse
    cId:="4ff4a1cd-1f8f-4436-8fb6-93e9769f3610"
    cPw:="fhlQo4u6929i21dyKwJa2w=="
    w_client_id=cId
    w_client_secret=cPw
    cUrl:="https://api-seguridad.sunat.gob.pe/v1/clientessol/"
    cUrl+=w_client_id
    cUrl+="/oauth2/token/"
    oHttp:Open( "POST", cUrl, .F.)
    ohttp:SetRequestHeader("content-type" , "application/x-www-form-urlencoded" )
    
    w_send := "grant_type=password&scope=https://api-sire.sunat.gob.pe&client_id="+w_client_id+"&client_secret="+w_client_secret+"&username=20554139834KREARINM&password=12345678"
    TRY
        ohttp:Send(w_send)   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END                  
    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    
    W_Token     := aHasRes["access_token"]

    ?"descargar propuesta"
    
    curl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rce/propuesta/web/propuesta/202305/exportacioncomprobantepropuesta?codTipoArchivo=0&codOrigenEnvio=1&fecEmisionIni=2023-05-01&fecEmisionFin=2023-05-31&codTipo CDP=01"
    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
            MsgAlert( XRespuesta, Ptitle )
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    
    W_NoTicket     := aHasRes["numTicket"]                  
    ?"consultar estado de ticket"
    cUrl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/gestionprocesosmasivos/web/masivo/consultaestadotickets?perIni=202305&perFin=202305&page=1&perPage=20&numTicket="+W_NoTicket

    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
            MsgAlert( XRespuesta, Ptitle )
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    aRegistros:=aHasRes["registros"]
    cEstado:=aRegistros[1]["desEstadoProceso"]
    cFile :=aRegistros[1]["archivoReporte"][1]["nomArchivoReporte"]

    ?"descargar archivo"

    cUrl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/gestionprocesosmasivos/web/masivo/archivoreporte?nomArchivoReporte="
    cUrl+=cFile+"&codTipoArchivoReporte=null"

    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
            MsgAlert( XRespuesta, Ptitle )
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    ?aHasRes
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 231
Joined: Fri Jul 20, 2012 01:49 AM
Re: Descargar archivo devuelto por un json
Posted: Sun Oct 22, 2023 03:00 AM
Hola,

Me parece que tiene un fichero como retorno, prueba guardar el archivo con extension .zip :D
Regards,

Lailton Fernando Mariano
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: Descargar archivo devuelto por un json
Posted: Sun Oct 22, 2023 04:48 PM

Eso es lo que quiero hacer descargar el archivo mediante codigo fw como lo hago?

fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Descargar archivo devuelto por un json
Posted: Sun Oct 22, 2023 05:28 PM
This is a working example:
Code (fw): Select all Collapse
#include "Fivewin.ch"


FUNCTION MAIN()

    ? URLDOWNLOADTOFILE( "https://www.emagsoftware.it/logo.png", "emaglogo.png" ) = 0

    RETURN NIL


#pragma BEGINDUMP

#include "hbapi.h"
#include "urlmon.h"
#include "wininet.h"


HB_FUNC( URLDOWNLOADTOFILE )
{
    DeleteUrlCacheEntry( hb_parc( 1 ) );

    hb_retnl( URLDownloadToFile( NULL, hb_parc( 1 ), hb_parc( 2 ), 0, NULL ) );
}

#pragma ENDDUMP
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: Descargar archivo devuelto por un json
Posted: Sun Oct 22, 2023 06:19 PM
Hola le agregue este codigo pero no reconoce el archivo
Code (fw): Select all Collapse
    ?"consultar estado de ticket"
    cUrl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/gestionprocesosmasivos/web/masivo/consultaestadotickets?perIni=202305&perFin=202305&page=1&perPage=20&numTicket="+W_NoTicket

    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
            MsgAlert( XRespuesta, Ptitle )
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    aRegistros:=aHasRes["registros"]
    cEstado:=aRegistros[1]["desEstadoProceso"]
    cFile :=aRegistros[1]["archivoReporte"][1]["nomArchivoReporte"]


 ?"descargar archivo"

    cUrl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/gestionprocesosmasivos/web/masivo/archivoreporte?nomArchivoReporte="
    cUrl+=cFile+"&codTipoArchivoReporte=null"

    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()   // ohttp:Send(enviar)
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:responseText
            MsgAlert( XRespuesta, Ptitle )
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
 HB_MEMOWRIT( cFile, XRespuesta )
   ShellExecute( 0, "Open", cFile )

/*    aHasRes := hash()
    hb_jsondecode(XRespuesta ,@aHasRes) //Parse JSON to hash
    ?aHasRes*/
Pero no reconoce que tipo de archivo es y no lo abre, sale este msje
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
Posts: 400
Joined: Fri May 11, 2007 08:20 PM
Re: Descargar archivo devuelto por un json
Posted: Mon Oct 23, 2023 06:09 AM
Gracias Enrico por responder pero lo resolvi de otra forma, dejo el codigo
Code (fw): Select all Collapse
    ?"descargar archivo"

    cUrl:="https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/gestionprocesosmasivos/web/masivo/archivoreporte?nomArchivoReporte="
    cUrl+=cFile+"&codTipoArchivoReporte=null"

    oHttp:Open( "GET", cUrl, .F.)
    ohttp:SetRequestHeader("Authorization" , "Bearer "+W_Token )
    ohttp:SetRequestHeader("Accept" , "application/json" )
    ohttp:SetRequestHeader("Content-Type" , "application/json" )
    TRY
        ohttp:Send()  
        IF oHttp:Status == 200
            Sysrefresh()
            XRespuesta:=ohttp:ResponseBody
        ENDIF
    CATCH oError
        MsgAlert( oError:Description, Ptitle )
        RETURN (.F.)
    END
    HB_MEMOWRIT( cfile, XRespuesta )
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql

Continue the discussion