Quickly Testing My Web API?

Discussion in 'Mac Programming' started by ArtOfWarfare, Jun 1, 2014.

  1. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    I'm writing an app for my server which has a RESTful API. Thus far, to quickly test features as I add them, I just use curl on the terminal to see if a POST or GET request is working properly. When they work properly, I get an easy to read JSON response. When it doesn't work, I get 2000 lines of HTML produced by Django. It's fairly tedious to have to copy and paste this response into a text file, save it, then open it in a web browser to view what the details of the error are.

    There has to be an easier way of doing this. I could probably write a small command line utility which would run curl for me, grep for </html> and respond with the JSON if it's not found, or save the results into a tmp file and open it in a browser if it does find that... in fact, that's probably easier done then said.

    Still looking to hear what solutions other people have.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    I'd probably do that. Sounds like a few lines of shell script to me.

    You can tell grep to not print matches, only set its exit status according to found/not found. Then the && or || conditionals can easily link an 'open savedFileHere.html' as a consequent of failing to match "</html>". And yeah, it's simpler to sketch that as shell commands than to describe it, e.g.:
    Code:
    curl "$@" | tee savedResponse.html
    grep -iq "</html>" || open savedResponse.html
    
    That's the gist of it, but I'd run it a few times and play around with it.

    I'd just put it in a shell script and add it to my ~/bin dir (which I've added to my PATH). If you don't want a ~/bin, there's probably a /usr/local/bin you can put it in (which you might need to mkdir first).
     
  3. robvas macrumors 68030

    Joined:
    Mar 29, 2009
    Location:
    USA
    #3
    We made a javascript program that just hits all the possible API requests and says if they passed or failed.
     
  4. ArtOfWarfare thread starter macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Yeah, we have a set of iOS unit tests that test the iOS API Framework, so if any of those tests fail we know either the iOS API is wrong, or more likely, the web API is wrong (because the iOS code is really simple, just taking the arguments and packing them into proper HTTP requests or getting responses and decoding them to objects).
     

Share This Page