#2710 WebClient POST Parameters

como Fri 31 Aug

Hello Fantom Users,

I am trying to create a connector to the Kaiterra API. the authentication scheme asks to send your key in a URL parameter called key.

I am new to the Fantom WebClient class so am trying to figure out. Based on the postForm documentation, it looks like this does most of the work for sending parameters. When I run my code, I get the following:

sys::IOErr: java.io.EOFException
  java.util.zip.GZIPInputStream.readUByte (Unknown)
  java.util.zip.GZIPInputStream.readUShort (Unknown)
  java.util.zip.GZIPInputStream.readHeader (Unknown)
  java.util.zip.GZIPInputStream.<init> (Unknown)
  java.util.zip.GZIPInputStream.<init> (Unknown)
  fan.sys.Zip.gzipInStream (Zip.java:223)
  web::WebUtil.makeContentInStream (WebUtil.fan:269)
  web::WebClient.readRes (WebClient.fan:491)
  web::WebClient.postForm (WebClient.fan:325)
  Main_0::Client.tester (/C:/Users/uscm670228/Github/BLDRLab/Fantom/examples/sensedgeTest/fan/Main.fan:19)
  Main_0::Client.main (/C:/Users/uscm670228/Github/BLDRLab/Fantom/examples/sensedgeTest/fan/Main.fan:11)
  java.lang.reflect.Method.invoke (Unknown)
  fan.sys.Method.invoke (Method.java:573)
  fan.sys.Method$MethodFunc.callOn (Method.java:244)
  fan.sys.Method.callOn (Method.java:139)
  fanx.tools.Fan.callMain (Fan.java:185)
  fanx.tools.Fan.executeFile (Fan.java:105)
  fanx.tools.Fan.execute (Fan.java:37)
  fanx.tools.Fan.run (Fan.java:308)
  fanx.tools.Fan.main (Fan.java:346)

The code is very simple, so maybe I am missing something.

using web

**
** Working with WebClient
**
class Client
{

  Void main()
  {
  	tester
  }

  Void tester()
  {
    // simple string get
    echo("\n--- getStr ---")
    myClient := WebClient(`https://api.origins-china.cn/v1/sensedges/<my_uuid>`)
    myClient.postForm(["key":"<my-key>"])
    echo(myClient.resStr)
    myClient.close
  }
}

I have python code that works as follows:

class CallKaiterra():
    def __init__(self, key, devices):
        self.key = key
        self.base_url = "https://api.origins-china.cn/v1/sensedges/"
        self.devices = devices
        self.headers = {'Accept-Encoding': 'gzip',
                        'Content-Encoding': 'UTF-8'}
        self.params = {'key': self.key}
        self.hist_params = {'key': self.key,
                            'series': 'raw'}
        self.data = {}

    def call(self, id, type):
        if type == "cur":
            response = requests.get(self.base_url + id,
                                    headers = self.headers,
                                    params = self.params)
        elif type == "hist":
            response = requests.get(self.base_url + id + "/history",
                                    headers = self.headers,
                                    params = self.hist_params)
        return response
    
    def get_cur_data(self):
        for k, v in self.devices.items():
            response = self.call(v, "cur")
            if response.status_code == 200:
                self.data[k] = json.loads(response.text)
    
    def get_hist_data(self, end, begin=None, limit=None):
        self.end = end
        if limit:
            self.params['limit'] = limit
        if begin:
            self.params['begin'] = begin
        response = self.call(self.devices["Boulder"], "hist")
        self.data["Boulder"] = json.loads(response.text)

if __name__ == '__main__':
    devices = {"Boulder": '<uuid_boulder>',
              "New York": '<uuid_new_york>'}
    c = CallKaiterra("<my_key>", 
                     devices)

If anybody has any insight, would be appreciated. Thanks.

brian Fri 31 Aug

It looks like the response is gzipped incorrectly.

I'd suggest using generic tool to dump the headers and see what they look like and if they server is saying the content is gzip encoded

Login or Signup to reply.