json-fortran icon indicating copy to clipboard operation
json-fortran copied to clipboard

json parse failed

Open vypivshiy opened this issue 1 year ago • 1 comments

Description

Hello! I used your library to make a telegram bot, but the response from the getMe method is parsed with an error.

System

OS: Linux Mint 21.2 x86_64 Kernel: 5.15.0-79-generic CPU: Intel i5-10400F (12) @ 4.300GHz Compiler: gfortran 11.4.0

Libraries used

Reproduction

without http_client

program main
  use json_module, only: json_file
  implicit none
  
  type(json_file)                 :: json
  character(:), allocatable  :: jsn_string 
  
  jsn_string = '{"ok":true,"result":{"id":12345678,"is_bot":true,' // &
             '"first_name":"pavel","username":"durov",' // &
             '"can_join_groups":true,"can_read_all_group_messages":false,' // &
             '"supports_inline_queries":false}}'
  call json%initialize()
  call json%deserialize(jsn_string)
  print*, json%failed()  ! T
end program

with http_client

program main
  use json_module, only: json_file
  use http, only : request, response_type, HTTP_POST, pair_type
  implicit none
  
  character(:), allocatable  :: TOKEN
  character(:), allocatable  :: api_url
  character(:), allocatable  :: payload
  type(json_file)                 :: json
  
  ! http variables
  type(pair_type), allocatable          :: req_header(:)
  type(response_type)                   :: response
  
  ! telegram bot valid token variable
  TOKEN = "TELEGRAM_BOT_TOKEN"
  
  ! prepare request
  payload = "{}"
  api_url = "https://api.telegram.org/bot"//TOKEN//"/getMe"
  req_header = [pair_type('Content-Type', 'application/json')]

  ! send request
  response = request(api_url, method=HTTP_POST, data=payload, header=req_header)
  
  call json%initialize()
  call json%deserialize(response%content)
  print*, json%failed()  ! T
end program

vypivshiy avatar Sep 02 '23 20:09 vypivshiy

The first one works fine for me. What compiler/platform are you using?

jacobwilliams avatar Sep 30 '23 22:09 jacobwilliams