What could be wrong with this syntax pharos?

366 Views Asked by At

I am trying to create a method that logs to a server, grabs the json file it found, then selects 4 of each element and sends it to an address. My code below seems to work when I give it only a single data known for each format in the send link rather than a loop that should select each at a time. The error I'm getting is: instance of smallInteger did not understand #readStream. What is causing this error? How else can I automate these requests?

1 to: 4 do: [ :each |
   each.
   a := ZnClient new.
   a get: 'https://MyServer/'.
   a headerAt: 'referer' put: 'https://MyServer' ;
     formAt: 'email' add: 'myEmail' ;
     formAt: 'password' add: 'MyPass'.
   a post.
   a get: 'https://MyServer/json'.

   data := NeoJSONReader fromString: a contents.
   list := data at: each.
   foo := list at: 'num'.
   poo := list at: 'name'.
    
   a get: 'https://MyServer/copy/', poo.
   a url: 'https://MyServer/send/'.
   a formAt: 'add' add: 'given address' ;
     formAt: 'nb_pic' add: foo ;
     formAt: 'identf' add: poo.

   a post.
   a get: 'https://MyServer/json' ]

2

There are 2 best solutions below

1
On

At first sight, there is nothing wrong with the syntax.
But looks like you didn't get the API of the framework you are using: you send get and post messages without understand that they will actually execute a "http get" and "http post" each time you send them.

So, while the "syntax" it self is ok, what is very incorrect is what you are doing (Which I do not understand what is). Look, this is how your program can be understood:

4 timesRepeat: [
    "this will do a post" 
    ZnClient new
        url: 'https://MyServer/';
        headerAt: 'referer' put: 'https://MyServer';
        formAt: 'email' add: 'myEmail';
        formAt: 'password' add: 'MyPass';
        post.

    "this is a simple get"
    a := ZnClient get: 'https://MyServer/json'.
    data := NeoJSONReader fromString: a contents.
    list := data at:each.
    foo := list at:'num'.
    poo := list at:'name'.

    "this is another get that I don't know what's doing here"
    a := ZnClient get: 'https://MyServer/copy/', poo.

    "this is another post"
    a := ZnClient 
        url: 'https://MyServer/send/';
        formAt: 'add' add: 'given address';
        formAt: 'nb_pic' add:foo;
        formAt: 'identf' add: poo;
        post.

    "and finally, this is another get"
    ZnClient get: 'https://MyServer/json' ]

clearly, that code is not doing what you want it to do :)

0
On

I figured out thanks to @Carlo hint that the error message: instance of smallInteger did not understand #readStream was due to the values collected from poo and foo.

    list := data at:each.
    foo := list at:'num'. "Here and integer"
    poo := list at:'name'."Here a byteString"

In fact the form action expects a key and value like shown, however the value has to be a string which I was not doing right by just substituting poo and foo to add:

    formAt: 'nb_pic' add:foo;
    formAt: 'identf' add: poo;

Therefore I needed to convert foo and poo asString and Now it works fine. Thanks