Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:39 29 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PicoMite Web Client Session.

Author Message
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 292
Posted: 11:47pm 18 Mar 2023
Copy link to clipboard 
Print this post

It feels like it should work...

WorldTimeAPI is a simple web service that returns the current local time for a given timezone as either plain text or JSON.

curl
"http://worldtimeapi.org/api/timezone/America/New_York"




And my code is as close to the SAMPLE code as possible for "reasons".


DIM buff%(512)
DIM  GetRequest$ = "GET /api/timezone/America/New_York"+Chr$(10)+Chr$(13)
DIM ADDRESS$ = "worldtimeapi.org"
 
'=====[ MAIN CODE ]=============================================================
'Do

print "Open Session"
WEB OPEN TCP CLIENT Address$,80
print "Make Request"
WEB TCP CLIENT REQUEST GetRequest$, buff%(),10000
print "Close Session"
WEB CLOSE TCP CLIENT
' Just dump out what I got back...
LongString Print buff%()




And what I get is:

> run
Open Session
Connected
Make request
Close Session
HTTP/1.1 400 Bad Request
content-length: 0
date: Sat, 18 Mar 2023 23:33:10 GMT



I'm kinda bummed as it seemed simple enough...
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6102
Posted: 01:33am 19 Mar 2023
Copy link to clipboard 
Print this post

DIM  GetRequest$ = "GET /api/timezone/America/new_york HTTP/1.1"+Chr$(13)+Chr$(10)+Chr$(13)+Chr$(10)


Case may be important. new_york all in lower case.
You need the 'HTTP/1.1" at the end
and you seems to need two crlf pairs.

Jim

Edit: Case is NOT important for the location.
Edited 2023-03-19 11:38 by TassyJim
VK7JH
MMedit   MMBasic Help
 
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 292
Posted: 10:18am 19 Mar 2023
Copy link to clipboard 
Print this post

Thanks  I must be blind... it was right there.

But its baby steps...

I now GET the response... but annoyingly, it's not just JSON in the buffer, as I got all of the header information too so JSON$ will "barf" on it. (I mean, It does, as it's essentially not JSON data if it sees the header info too)

What the Client session gets back in the BUFF%() from the server is:

Make Request
Close Session
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
access-control-expose-headers:  
cache-control: max-age=0, private, must-revalidate
content-length: 398
content-type: application/json; charset=utf-8
cross-origin-window-policy: deny
date: Sun, 19 Mar 2023 10:34:52 GMT
server: Fly/00340618 (2023-03-12)
x-content-type-options: nosniff
x-download-options: noopen
x-frame-options: SAMEORIGIN
x-permitted-cross-domain-policies: none
x-request-id: F03LmMh6G30W6Pos4tah
x-runtime: 782�s
x-xss-protection: 1; mode=block
via: 1.1 fly.io
fly-request-id: 01GVWPZDA8QTW11HZG5Q7AF5P6-atl

{"abbreviation":"EDT","client_ip":"98.251.103.80","datetime":"2023-03-19T06:34:53}
>


Which clearly is not what I expect, what is documented... nor what my API testing tool showed me. So... <sigh>... there are still secrets to uncover.
Edited 2023-03-19 20:41 by pwillard
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9139
Posted: 11:12am 19 Mar 2023
Copy link to clipboard 
Print this post

LongString trim buff%(),LInStr(buff%(),"{")-1

Edited 2023-03-19 21:12 by matherp
 
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 292
Posted: 11:37am 19 Mar 2023
Copy link to clipboard 
Print this post

Ok well... That *does* trim out the header... (Which was where I was headed with this...  so Thanks)

It seems ODD that the header gets captured on the GET REQUEST made by MMBASIC, but not on a CURL or an "Advanced Rest Client" request. (or are we SUPPOSED to capture header data in the GET request?)

I thought this would be a simple client API call example to add to my docs... but it quickly became troublesome.  Anyway... JSON$ is happy now as the BUFF%() array now does only have JSON data in it.

Sample Code:


' Program:      TimeApi.bas
' Author:       pete willard
' Version:      1.0
' Target:       PicoWeb
' Date:         2023/03/19
' Time:         07:22:29
' Reference:    
'===============================================================================
' TTTTT  III  M   M EEEEE  AAA  PPPP   III        BBBB   AAA   SSSS
'   T     I   MM MM E     A   A P   P   I         B   B A   A S    
'   T     I   M M M EEEE  AAAAA PPPP    I         BBBB  AAAAA  SSS  
'   T     I   M M M E     A   A P       I    ..   B   B A   A     S
'   T    III  M   M EEEEE A   A P      III   ..   BBBB  A   A SSSS  
'===============================================================================
' Notes:  USING    "Advanced Rest Client App"    
' Get:
'               "http://worldtimeapi.org/api/timezone/America/New_York"
' Receives:
'{
 '"abbreviation": "EDT",
 '"client_ip": "98.251.103.80",
 '"datetime": "2023-03-18T11:56:00.035491-04:00",
 '"day_of_week": 6,
 '"day_of_year": 77,
 '"dst": true,
 '"dst_from": "2023-03-12T07:00:00+00:00",
 '"dst_offset": 3600,
 '"dst_until": "2023-11-05T06:00:00+00:00",
 '"raw_offset": -18000,
 '"timezone": "America/New_York",
 '"unixtime": 1679154960,
 '"utc_datetime": "2023-03-18T15:56:00.035491+00:00",
 '"utc_offset": "-04:00",
 '"week_number": 11
'}  
' For some reason, the API call wants 2 carriage returns on a GET request.
'=====[ CONFIGURATION ]=========================================================
'OPTION SYSTEM SPI GP18, GP19, GP16
'OPTION SYSTEM I2C GP0, GP1
'OPTION SDCARD GP22
'OPTION DEFAULT INTEGER
'OPTION EXPLICIT

'=====[ VARIABLES ]=============================================================

DIM buff%(512) ' Response Buffer
DIM CRLF$ = CHR$(10)+CHR$(13)
DIM GetRequest$ = "GET /api/timezone/America/New_York HTTP/1.1"+CRLF$+CRLF$
DIM ADDRESS$ = "worldtimeapi.org"

'=====[ SUBROUTINES ]===========================================================

'===============================================================================
 


'=====[ MAIN CODE ]=============================================================


print "Open Session"
WEB OPEN TCP CLIENT Address$,80
print "Make Request"
WEB TCP CLIENT REQUEST GetRequest$, buff%(),10000
print "Close Session"
WEB CLOSE TCP CLIENT

' Trim result
LongString trim buff%(),LInStr(buff%(),"{")-1
'Print result
'longstring print buff%()

print "Date and Time: ",Json$(buff%(),"datetime")
Print "TimeZone     : ",Json$(buff%(),"timezone")
Print "UTC Offset   : ",Json$(buff%(),"raw_offset")

' Note: RUN ONCE... this is an API so don't abuse it.

'===============================================================================






Results:
RUN
Open Session
Connected
Make Request
Close Session
Date and Time:         2023-03-19T07:35:33.786172-04:00
TimeZone     :         America/New_York
UTC Offset   :         -18000


woohoo!
Edited 2023-03-19 21:44 by pwillard
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024