Blockland Forums > Modification Help

write a for loop in order to get a response from an api

Pages: << < (2/3) > >>

Latte:

Assuming the API you're trying to use returns a content type of application/json (99% of them), you're going to have a hard time trying to use TCPClient because it thinks everything that doesn't match text/* is binary. So you're going to need to have it save to a file and read it in onDone.

Crøwn:


--- Quote from: theviacom on November 24, 2017, 02:12:33 PM ---so then connectToUrl basically passes the result on to myAPIRequest which will be accessed by doing myAPIRequest::handleText, and %text stores the text itself?

--- End quote ---
Yeah pretty much, and based on the example in the OP I got the feeling this isn't really an API but just a php page that outputs text when given certain GET variables. If this is the case just using what I posted will be faster than saving a file and reading it. It would be helpful if you posted the php code here or at least the webpage you're trying to access.

Also should note that handleText gets called for every line, and might contain header information or other stuff you might not want (not sure how TCPClient handles that) but be aware. You might have to do something like "response: your data here" and only grab lines that begin with "response:".

Latte:

I messaged Greek2me and he fixed it in version 15, so it should be really, really simple now: http://www.greek2me.us/code/Support_TCPClient.cs


--- Code: ---function myRequest() {
    connectToURL("http://jsonplaceholder.typicode.com/posts/1", "GET", "", "myAPIRequest");
}

function myAPIRequest::handleText(%this, %text) {
    %this.text = %this.text @ %text;
}

function myAPIRequest::onDone(%this, %error) {
    if (!%error) {
        echo("JSON: " @ %this.text);
    }
}
--- End code ---



Platypi:


--- Quote from: theviacom on November 24, 2017, 12:44:04 AM ---https://github.com/qoh/jettison

--- End quote ---
Haven't tried this add-on out yet, but it's already making me pre-cum a little.

Kyuande:

The BLG add-on uses jettison very often to send objects.

Pages: << < (2/3) > >>

Go to full version