简体   繁体   中英

How can I create a simple api to test my mobile app without having to code the api out on a server?

I am making an android and iOS app that is going to interact with a server using an api that returns appropriate json responses. Someone else on my team is doing the server side work. I don't want to bother/wait for her to finish the server implementation.

Is there a website out there that will help me create a test api so that whenever I call it, it will return a predefined Json response?

Example: if the website is called api.com and so if I create an account and call GET api.com/my_account/get_key it returns {"key": "fgjllpoiunvxaqw"} . I will hardcode the response on the website of course.

Thanks :)

If you are just reading data from the server (eg making only /GET requests) you can put some .json files on server and just read them from your app.

For example, for get_key, you could call GET yourwebsite.com/get_key.json and parse the data. This way you can simulate how your app works with network delays, which is very useful to start adding loaders and error handling UI.

If you need to POST, I usually have a PHP script to write to a file on the server, to check later what it's being posted:

<?php
   file_put_contents('testPostUserData.txt', file_get_contents('php://input'));
?>

If you require more dynamic interaction (GET and POST data, retrieve some data for a specific user) then you can use some of the services available for free like Backendless , Kinvey , StackMob , or Parse .

Finally, I usually have a preprocessor #define to alternate between fake API and real API

#ifdef FAKE_API
static NSString * path requestBaseURL = @"http://yourwebsite.com";
#else
static NSString * const requestBaseURL = @"http://realwebserver.com";
#endif

That would be very simple to implement - what you need is a mock API. There's several around, like JMockit and Mockito .

Mocking is an integral part of software design and testing.

Although this solution requires you to code a little, I think it may be good enough for some.

While I was waiting for a response, I wrote a mock api using BottlePy and it was ridiculously easy for a new python developer like me.

Step 1: easy_install bottle or pip install bottle
Step 2: create your API as such:

from bottle import route, run
import json

@route('/login', method='POST')
def login():
    if request.POST.get('username') == 'a' and request.POST.get('password') == 'b':
        return json.dumps({'status': 'success'})
    else:
        return json.dumps({'status': 'fail'})

run(host='localhost', port=8080, reloader=True)
# THATS IT!!!!

Step 3: Run the file using your console python name_of_file.py
Step 4: DONE, your api is up. Test using POST localhost:8080/login in this case

Finally found a really good website to do this: Apiary . Does exactly what I needed, plus a few other awesome things like creating documentation, providing collaboration tools, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM