Descargar archivo devuelto por un json (Solucionado)

Descargar archivo devuelto por un json (Solucionado)

Postby artu01 » Sun Oct 22, 2023 1: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
Image

Hice la prueba en postman y sale esto
Image

Como hago para descargar el archivo mediante fwh?

Pego mi cod. fuente, espero me ayuden, gracias
Code: Select all  Expand view

    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


 
Last edited by artu01 on Mon Oct 23, 2023 6:09 am, edited 1 time in total.
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
artu01
 
Posts: 397
Joined: Fri May 11, 2007 8:20 pm
Location: Lima

Re: Descargar archivo devuelto por un json

Postby Lailton » Sun Oct 22, 2023 3:00 am

Hola,

Me parece que tiene un fichero como retorno, prueba guardar el archivo con extension .zip :D
Regards,
Lailton Fernando Mariano
User avatar
Lailton
 
Posts: 125
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil

Re: Descargar archivo devuelto por un json

Postby artu01 » Sun Oct 22, 2023 4: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
artu01
 
Posts: 397
Joined: Fri May 11, 2007 8:20 pm
Location: Lima

Re: Descargar archivo devuelto por un json

Postby Enrico Maria Giordano » Sun Oct 22, 2023 5:28 pm

This is a working example:

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Descargar archivo devuelto por un json

Postby artu01 » Sun Oct 22, 2023 6:19 pm

Hola le agregue este codigo pero no reconoce el archivo
Code: Select all  Expand view


    ?"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
Image
fwh 17.12, harbour 3.2.0, pelles C, bcc7, Ms-Sql
artu01
 
Posts: 397
Joined: Fri May 11, 2007 8:20 pm
Location: Lima

Re: Descargar archivo devuelto por un json

Postby artu01 » Mon Oct 23, 2023 6:09 am

Gracias Enrico por responder pero lo resolvi de otra forma, dejo el codigo
Code: Select all  Expand view

    ?"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
artu01
 
Posts: 397
Joined: Fri May 11, 2007 8:20 pm
Location: Lima


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 84 guests