Home AMX User Forum AMX General Discussion

NetLinx HTTP Client

As we are yet to get any support from inbuilt NetLinx libraries for working with HTTP, here's a little tool for those who may not want to push all of these comms through Duet.

https://github.com/ciasia/http

The http lib is a minimal, performant HTTP client for working with web services natively from NetLinx. Methods are provided for GET, HEAD, PUT, POST, PATCH and DELETE requests over HTTP/1.1. In it's vanilla form it will support up to 15 simultaneous requests however this can be expanded to match your requirements. Callbacks (following the RMS SDK callback pattern) are provided for all responses and errors, including communication timeouts and socket errors. These callbacks are also given a pre-parsed response with everything neatly separated into a structure containing the response code, headers and body. More info in the readme.

For working with JSON data within the requests or PUT/POST/PATCH bodies you may also find the jsmn library and json-builder include of use.

Feel free to fork, improve and optimise at will. If you come across any issues, but don't have the time to build out a fix, it would be awesome if you could log the issue on GitHub so someone from the community can fix it up for all.

Comments

  • filpeefilpee Posts: 64
    As always, nice work!
  • AvargasAvargas Posts: 57
    You rock, sir.
  • ericmedleyericmedley Posts: 4,177
    "I'm in your GitHub - stealin yur codes..." Thanks mang!
  • jabramsonjabramson Posts: 106
    Anyone have a working example of this code that I can borrow?
  • ericmedleyericmedley Posts: 4,177
    jabramson wrote: »
    Anyone have a working example of this code that I can borrow?


    It's in the Git repo listed in the init of the thread
  • jabramsonjabramson Posts: 106
    ericmedley wrote: »


    It's in the Git repo listed in the init of the thread

    I just saw a description of the functions, not a fully functioning example.
  • ericmedleyericmedley Posts: 4,177
    jabramson wrote: »

    I just saw a description of the functions, not a fully functioning example.
    I see a file named http.axi in the repo. It's has a pretty basic and well written set of code that works.
  • mushmush Posts: 287
    G'day Kim,

    Forgive my ignorance but how can I push back into your repository?
    Do I make a pull request or something like that?

    Cheers

    Mush
  • PhreaKPhreaK Posts: 966
    You got it. Check out GitHub's intro to pull requests if you're new to it.

    P.S. thanks in advance.
  • mushmush Posts: 287
    Ha ha, don't be too thankful just yet. I'm only making it easier for simple minded folk like myself to get started!
  • Has anyone experienced the error 'HTTP lib resources at capacity. Request dropped.'? I figure I'm getting this error because I am not getting a resource_id number...any help would be greatly appreciated!

    Edit: Never mind, fixed it! Thanks again for the great chunk of code [USER="6496"]PhreaK[/USER]
  • PhreaKPhreaK Posts: 966
    Nice to hear it's up and going. What was the issue btw?

    Just for the benefit of anyone else reading along, there is not currently any request buffering implemented. You'll see that message logged if you attempt to exceed the number of simultaneous requests allowed. This can however be changed to suit your application (e.g. lots of requests, slow web server etc). Under normal conditions requests will timeout 10 seconds after they are executed. If you are getting this repeatedly, it means you have a socket stuck open and something has gone wrong so let me know.
  • I believe the issue was throwing Command A and Command B both on a button_event release. (Even with a wait separating the two...)

    After separating the two commands to a push and release, that seemed to do the trick.

    Although, this solution doesn't sound right to me...but I may be missing something.

    The other possibility is it had something to do with the network I was on, as changes were made in the time between the problem happening and the problem disappearing.

    But, if my understanding of the code is correct, I get that error message due to a lack of RequestID...what I don't understand is if that RequestID is generated by the AXI or by the server.

    Apologies for my ignorance...I wish I had a better explanation!
  • I was playing around with this yesterday and I was running into trouble getting it to work with Slack. According to their API, I should be able to send an HTTP JSON POST request and it will show up in a channel. After playing around, it seems to send me back an HTTP 302, which is redirect. I'm thinking it is trying to redirect the http request to https? I'm not sure, I don't have a lot of experience in this area. Does this client support https? Or does anyone have any ideas?
  • If anyone has an example of this of any kind, that would be awesome. I realize it may seem straight forward to folks who have experience with http but I'm sure many others could benefit from an example. Thanks in advance to anyone who can help!!
  • I was just going to dive in and try things out. OK, I haven't got anything (http wise) to try it out with, but hey ho.

Sign In or Register to comment.