Resultados 1 al 5 de 5

Tema: HttpRequest

  1. #1
      Guardián del Foro
    Avatar de Fito

    Registrado
    febrero de 2015
    Ubicación
    Córdoba, Villa María
    Edad
    53
    Mensajes
    410
    Última visita
    Ayer a las 17:17

    Agradecimientos
     
    Recibidos
    208
    Enviados
    225

    Citaciones y menciones
     
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    3 Post(s)
    Poder de Reputación
    26
    Agradecimientos / Point Value: 0 Guardián del Foro / Point Value: 0 Activista del Foro / Point Value: 0

    Gadget HttpRequest

    Hola Amigos:

    Estoy haciendo una aplicación para conectar mi sistema a un servicio web (Conquer) para vendedores (notas de pedido, cobranzas, etc).

    Estoy usando HttpRequest. logro hacer la conexión, pedir el token, pero después tengo que enviar un archivo json con la tabla de artículos.

    Les voy pasando código:

    COBOL Código:
    1.  01  variables-conexion global.
    2.  
    3.      02 winhttp          pic x(128) value "WinHttp.WinHttpRequest.5.1".
    4.  
    5.      02 oWS usage object reference COM.
    6.      02 SRC              pic x(1024).
    7.      02 MSG              pic x(1024).
    8.  
    9.      02 metodo           pic x(1024) value "POST".
    10.      
    11.      02 autenticacion    pic x(1024) value "http://157.230.55.48:5000/api/v1/auth/login".
    12.      
    13.      02 arti-alta        pic x(1024) value "http://157.230.55.48:5000/api/v1/erp/articles/sync".
    14.      
    15.      02 clave            pic x(30)  value "XXXXXXXXX".
    16.  
    17.      02 credenciales.
    18.         03               pic x(7) value "Bearer ".
    19.         03 token         pic x(1024).
    20.          
    21.      02 RH1              pic x(1024).
    22.      02 RH2              pic x(1024).
    23.      
    24.      02 url              pic x(1024) value "http://157.230.55.48:5000/api/v1/erp/".
    25.      
    26.      02 modo             pic x(1024) value "FALSE".
    27.  
    28.      02 respuesta        pic x(8192).
    29.      
    30.      02 envio-r.
    31.         03                      pic x(19) value "d:\sernick\prueba1".
    32.         03 archivo-json.
    33.            04                   pic x(5) value "temp".
    34.            04 arch-json-time    pic 9(8).
    35.            04                   pic x(5) value ".json".
    36.      02 redefines envio-r.
    37.         03 envio                pic x(37).
    Estas son las variables que uso. El campo clave es la clave para pedir el token (no a pongo acá porque le pertenece al cliente)

    COBOL Código:
    1.  PROCEDURE       DIVISION.
    2.  
    3.  #include "copys\declara-com.cpy".
    4.  
    5.  comienzo.
    6.      invoke COM "CREATE-OBJECT" using winhttp returning oWS.
    7.  
    8.      invoke oWS "OPEN" using metodo autenticacion modo.
    9.  
    10.      move "Authorization"         to RH1.
    11.      move clave                   to RH2.
    12.      invoke oWS "setRequestHeader" using RH1 RH2.
    13.  
    14.      invoke oWS "send".
    15.      
    16.      invoke oWS "get-Responsetext" returning respuesta.
    17.  
    18.      set oWS           to null.
    19.  
    20.      invoke json "Clear".
    21.      
    22.      invoke json "Load" USING respuesta.
    23.      
    24.      invoke json "StringOf" using "token" returning token.
    25.      
    26.      invoke grdLista "AddItem" using respuesta.
    27.  
    28.      exit program.
    Con esto pido el token...

    COBOL Código:
    1.  WORKING-STORAGE SECTION.
    2.  01  wsend.
    3.      02       pic x(115) value '{"000001":{"k":"1","rk":"1","b":"0","d":"CREMOSO LA LACTEO","i":"","n":"CREMOSO LA LACTEO","ld":"CREMOSO LA LACTEO"'.
    4.      02       pic x(115) value ',"nc":"0","fa":"0","dum":"UN","v":{"a":"21","f":false},"um":{"UN":{"e":"1","k":"UN","n":"UNIDAD"}}}}'.
    5.  
    6.  PROCEDURE       DIVISION.
    7.  
    8.  #include "copys\declara-com.cpy".
    9.  
    10.  comienzo.
    11.      invoke COM "CREATE-OBJECT" using winhttp returning oWS.
    12.  
    13.      invoke oWS "OPEN" using metodo arti-alta modo.
    14.  
    15.      move "Authorization"         to RH1.
    16.      move credenciales            to RH2.
    17.      invoke oWS "setRequestHeader" using RH1 RH2.
    18.  
    19.      invoke grdLista "AddItem" using credenciales.
    20.  
    21.      move "Content-Type"          to RH1.
    22.      move "application/json"      to RH2.
    23.      invoke oWS "setRequestHeader" using RH1 RH2.
    24.  
    25.      invoke oWS "send" using wsend.
    26.      
    27.      invoke oWS "get-Responsetext" returning respuesta.
    28.  
    29.      set oWS           to null.
    30.  
    31.      invoke grdLista "AddItem" using respuesta.
    32.  
    33.      exit program.
    Acá trato de enviar el json de artículos. En esta prueba que les muestro el json lo cargo en una variable, pero la idea es poder enviar el archivo.

    Si lo hago con Postman no tengo problemas, pero no sé traducir al cobol eso

    Ahí les paso la imagen de pantalla del Postman.
    En el body, selecciono binary y me pide el archivo. Ahora cómo hacerlo en Cobol no pude...

    Saludos

    Fito...
    Imagem Anexada Imagem Anexada

    0 Not allowed!

  2. #2
      Administrador
    Avatar de Kuk

    Registrado
    enero de 2015
    Ubicación
    Madrid
    Edad
    39
    Mensajes
    2,286
    Última visita
    Ayer a las 21:52

    Agradecimientos
     
    Recibidos
    1,036
    Enviados
    888

    Citaciones y menciones
     
    Mentioned
    102 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    31 Post(s)
    Poder de Reputación
    10

    Predeterminado
    ¡Solución!1 usuarios han marcado el post como Solución

    @Fito, mira esto a ver: Post File HTTP using visual foxpro

    VB Código:
    1. filecontent = FileToStr("Movies.txt")
    2. loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
    3. loHTTP.Open("POST", "http://myip:8080/upload/", .F.)
    4. loHTTP.SetRequestHeader("content-type", "text/plain") && or other mime types for other file types.
    5. loHTTP.SetRequestHeader("content-disposition", 'attachment; filename="Movies.txt"')
    6. loHTTP.Send(filecontent)

    0 Not allowed!
    ¿Te han ayudado? NO TE OLVIDES de darle al botón
    ¿Quieres dirigirte a alguien en tu post? Notifícale con una mención, tienes 2 opciones:
    1. Haciendo clic en el icono al lado de su nick
    2. Haciendo clic en el botón en el editor y escribiendo su nick.

  3. #3
      Guardián del Foro
    Avatar de Fito

    Registrado
    febrero de 2015
    Ubicación
    Córdoba, Villa María
    Edad
    53
    Mensajes
    410
    Última visita
    Ayer a las 17:17

    Agradecimientos
     
    Recibidos
    208
    Enviados
    225

    Citaciones y menciones
     
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    3 Post(s)
    Poder de Reputación
    26
    Agradecimientos / Point Value: 0 Guardián del Foro / Point Value: 0 Activista del Foro / Point Value: 0

    Predeterminado

    Hola Kuk:

    Por lo que veo, en el método SEND va el contenido del archivo no el nombre.

    Lo que me parece raro es que si es así que va el contenido, para qué le decimos el nombre del archivo en el Header.

    Voy a probar así y comento.

    Muchas Gracias.

    Saludos.

    Fito...

    ---------- Post añadido el 1 de noviembre de 2021 a las 21:36 ----------

    Kuk, sos un genio...

    Así funcionó

    COBOL Código:
    1.  comienzo.
    2.      invoke COM "CREATE-OBJECT" using winhttp returning oWS.
    3.  
    4.      invoke oWS "OPEN" using metodo arti-alta modo.
    5.  
    6.      move "Authorization"         to RH1.
    7.      move credenciales            to RH2.
    8.      invoke oWS "setRequestHeader" using RH1 RH2.
    9.  
    10.      invoke grdLista "AddItem" using credenciales.
    11.  
    12.      move "Content-Type"          to RH1.
    13.      move "application/json"      to RH2.
    14.      invoke oWS "setRequestHeader" using RH1 RH2.
    15.  
    16.      move "Content-Disposition"   to RH1.
    17.      move spaces                  to RH2.
    18.      string 'attachment; filename="' delimited by size
    19.             envio                    delimited by size
    20.             '"'                      delimited by size
    21.         into RH2
    22.      end-string.
    23.      invoke oWS "setRequestHeader" using RH1 RH2.
    24.  
    25.      invoke oWS "send" using wjson.
    26.      
    27.      invoke oWS "get-Responsetext" returning respuesta.
    28.  
    29.      set oWS           to null.
    30.  
    31.      invoke grdLista "AddItem" using respuesta.
    32.  
    33.      exit program.

    En el campo envío tengo el nombre del archivo json y en el campo wjson tengo metido el contenido del json.

    El json lo genero con chilkat (tengo un post paralelo donde Fastpho me ha dado todas las soluciones). El activex de chilkat tiene un método para cargar en un string todo el contenido del json, pero me encuentro con el límite de 8192 caracteres, así que por ahora lo que hago es leer el archivo line sequential e ir cargando el string.

    Lo que sigo sin entender por qué en la cabecera le digo que adjunto el archivo json y después en el body en vez de hacer referencia al archivo, tengo que meter el contenido.

    Brillante amigo.

    Saludos

    Fito...

    0 Not allowed!

  4. #4
      Administrador
    Avatar de Kuk

    Registrado
    enero de 2015
    Ubicación
    Madrid
    Edad
    39
    Mensajes
    2,286
    Última visita
    Ayer a las 21:52

    Agradecimientos
     
    Recibidos
    1,036
    Enviados
    888

    Citaciones y menciones
     
    Mentioned
    102 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    31 Post(s)
    Poder de Reputación
    10

    Predeterminado

    Cita Iniciado por Fito Ver Mensaje
    Lo que sigo sin entender por qué en la cabecera le digo que adjunto el archivo json y después en el body en vez de hacer referencia al archivo, tengo que meter el contenido.
    Hola compañero. La verdad es que yo tampoco, este campo no es mi punto fuerte.
    Lo importante es que funciona

    0 Not allowed!
    ¿Te han ayudado? NO TE OLVIDES de darle al botón
    ¿Quieres dirigirte a alguien en tu post? Notifícale con una mención, tienes 2 opciones:
    1. Haciendo clic en el icono al lado de su nick
    2. Haciendo clic en el botón en el editor y escribiendo su nick.

  5. #5
      Senior
    Avatar de fastpho

    Registrado
    diciembre de 2016
    Ubicación
    Mendoza
    Edad
    53
    Mensajes
    326
    Última visita
    18.04.2024

    Agradecimientos
     
    Recibidos
    226
    Enviados
    209

    Citaciones y menciones
     
    Mentioned
    49 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    15 Post(s)
    Poder de Reputación
    21
    Innovación / Point Value: 0 Concurso: Primer puesto / Point Value: 0

    Predeterminado

    Cita Iniciado por Fito Ver Mensaje
    COBOL Código:
    1.  WORKING-STORAGE SECTION.
    2.  01  wsend.
    3.      02       pic x(115) value '{"000001":{"k":"1","rk":"1","b":"0","d":"CREMOSO LA LACTEO","i":"","n":"CREMOSO LA LACTEO","ld":"CREMOSO LA LACTEO"'.
    4.      02       pic x(115) value ',"nc":"0","fa":"0","dum":"UN","v":{"a":"21","f":false},"um":{"UN":{"e":"1","k":"UN","n":"UNIDAD"}}}}'.
    5.  
    6.  PROCEDURE       DIVISION.
    7.  
    8.  #include "copys\declara-com.cpy".
    9.  
    10.  comienzo.
    11.      invoke COM "CREATE-OBJECT" using winhttp returning oWS.
    12.  
    13.      invoke oWS "OPEN" using metodo arti-alta modo.
    14.  
    15.      move "Authorization"         to RH1.
    16.      move credenciales            to RH2.
    17.      invoke oWS "setRequestHeader" using RH1 RH2.
    18.  
    19.      invoke grdLista "AddItem" using credenciales.
    20.  
    21.      move "Content-Type"          to RH1.
    22.      move "application/json"      to RH2.
    23.      invoke oWS "setRequestHeader" using RH1 RH2.
    24.  
    25.      invoke oWS "send" using wsend.
    26.      
    27.      invoke oWS "get-Responsetext" returning respuesta.
    28.  
    29.      set oWS           to null.
    30.  
    31.      invoke grdLista "AddItem" using respuesta.
    32.  
    33.      exit program.
    Acá trato de enviar el json de artículos. En esta prueba que les muestro el json lo cargo en una variable, pero la idea es poder enviar el archivo.

    Si lo hago con Postman no tengo problemas, pero no sé traducir al cobol eso

    Ahí les paso la imagen de pantalla del Postman.
    En el body, selecciono binary y me pide el archivo. Ahora cómo hacerlo en Cobol no pude...

    Saludos

    Fito...
    Podrias probar algo como esto directamente codigo no probado

    VB Código:
    1. string
    2.                            '   Sub FuncionEnviarArticulo()                                      ' vbCRLF
    3.                           '        Dim RespEstado  , respuesta , success                       ' vbCRLF  
    4.                           '        Dim wsend , metodo arti-alta modo                           ' vbCRLF  
    5.                            '        Dim RH1 , RH2                                               ' vbCRLF                           
    6.  
    7.                            '        set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1") ' vbCRLF
    8.  
    9.                            '                                                                    ' vbCRLF
    10.  
    11.                            '     WinHttpReq.Open "POST", "metodo & arti-alta ",modo)             ' vbCRLF
    12.                           '                                                                    ' vbCRLF                          
    13.                           '        RH1 =   "Authorization"                                     ' vbCRLF                          
    14.                            '        RH2 =   "credenciales"                                      ' vbCRLF                          
    15.                            
    16.                            '     WinHttpReq.setRequestHeader ( RH1 , RH2 )                      ' vbCRLF                           
    17.  
    18.                            '        RH1 =   "Content-Type"                                      ' vbCRLF                          
    19.                            '        RH2 =   "application/json"                                  ' vbCRLF                          
    20.                            
    21.                            '     WinHttpReq.setRequestHeader ( RH1 , RH2 )                      ' vbCRLF                           
    22.                          
    23.                            '         set json = CreateObject("Chilkat_9_5_0.JsonObject")        ' vbCRLF
    24.                           '         success = json.LoadFile("plu.txt")                         ' vbCRLF * archivo con formato json ya creado  
    25.                           '         wsend = json.emit                                          ' vbCRLF *> variable con el registro json  
    26. *                          '         msgbox success                                             ' vbCRLF *> 1 correcto 0 error
    27.  
    28.  
    29.                            '     WinHttpReq.Send(wsend)                                         ' vbCRLF  *> envia los bloques      
    30.  
    31.                            '     WinHttpReq.WaitForResponse()                                   ' vbCRLF
    32.  
    33.                            '     respuesta = WinHttpReq.ResponseText                             ' vbCRLF
    34.  
    35.                            '          msgbox respuesta                                           ' vbCRLF
    36.                          
    37.                            '         RespEstado =  WinHttpReq.Status                            ' vbCRLF
    38.                          
    39.                            '         Estado   =  RespEstado                                     ' vbCRLF *> estado transferencia de bloques
    40.                          
    41.                            
    42.  
    43.                            
    44.                            '   End Sub                                                          ' vbCRLF                  
    45.                     delimited by size into scriptCode1
    46.                      end string
    47.       INVOKE scriptControl1 "Reset".
    48.      
    49.       invoke scriptControl1 "AddCode" using scriptCode.
    50.  
    51.       invoke scriptControl1 "ExecuteStatement" using "FuncionEnviarArticulo".

    0 Not allowed!

Información de Tema

Usuarios Viendo este Tema

Actualmente hay 1 usuarios viendo este tema. (0 miembros y 1 visitantes)

Marcadores

Marcadores

Permisos de Publicación

  • No puedes crear nuevos temas
  • No puedes responder temas
  • No puedes subir archivos adjuntos
  • No puedes editar tus mensajes
  •