Generate mock service as HTTP endpoint - android

I am using this example login to build the RESTful part in my application. However, I do not have a server connected. What I want to do is to create a mock service, using an HTTP generator as Mocky (check it). Does anyone know how can this be achieved? Thanks a lot!

Sorry for being so long to answer (little bit busy that week). Well, I hope you haven't given up meanwhile...
So below is the procedure to set up a RESTful mock service :
You can download the Opensource version of SOAP UI here. It's enough for making a simple mock service
Create a new empty Project
Right click on the project and click on "New REST mock service"
Right click on MockService1 and click on "Add new mock action"
Then double-click on /api/v1/user and Add a new mock response (right-click / New Mock response)
It's done. You just need to double-click on "REST MockService1" and run the service (green button)
You can configure the path, port and host :
Once running, you're WebService can be called using this HTTP Request : GET http://localhost:8080/api/v1/users
Much more information on the WebSite : https://www.soapui.org/rest-testing-mocking/rest-service-mocking.html
You can for example, define some algorithmes to make your webservice more intelligent and test every case (for example if user is = 1 return 200 OK {user} otherwise return 404 ERROR NOT FOUND, ...)

Related

Detect device/app on Rest call

I have a rails project whose API's are used by two different but related apps.
One app (Parent) is made using Unity, while other one (Child) is with native (iOS & Android).
In case of error response.
Unity app requires 2xx series status code,in case of any other status code they read it as success case.
While native (iOS & Android) apps need 4xx series status code, in case of any other status code they read it as success case.
Is there any way that from request I can know that which app sent request?
or any other solution to handle this?
Is there any way that from request I can know that which app sent
request?
Yes. Several ways.
1.Use form to send which device is making the request then access this fro your rail server.
WWWForm form = new WWWForm();
//From Unity
form.AddField("App", "Unity");
Or
//From Native
form.AddField("App", "Native");
Then send:
UnityWebRequest uwr = UnityWebRequest.Post(url, form);
yield return uwr.SendWebRequest();
2.Use a custom header to send which device is making the request then access this from your rail server.
//From Unity
UnityWebRequest.SetRequestHeader("App", "Unity");
Or
//From Native
UnityWebRequest.SetRequestHeader("App", "Native");
All you need to do is access "App" on rail for both of these.
3.Use json or xml to store which device the request is coming from the access this from the server.
The API used here is for Unity but you can do similar thing on iOS and Android too for the native app with Object-C and Java API. It's still the-same thing.
request method has parameter as
request.user_agent

Tasker App: Why my HTTP POST is not sending parameters?

I'm making a simple HTTP POST request using Tasker app, it's working fine but somehow it's not sending my arguments. HTTP GET is working fine. What am I doing wrong?
The only thing the server does is JSON stringify all received parameters and return them like: Text: { parameters }
HTTP GET is working:
HTTP POST works, it returns the response "text: {}" but doesn't return any parameter:
Now sometimes it shows "Bad Request Error 400"
I've tested it with another server and this issue persists...
Here's a fiddle to send get and post requests to the same server and both work.
I've exported my task as xml for you to test: You can download it here
Thanks in advance guys!
I have done a little testing with what you have supplied. It appears that your post parameters are making it to the server but %HTTPR is not getting populated. The Tasker documentation says that this variable will be populated if the response is "text-based", but this does not seem to be the case. I may not know what Tasker considers to be "text-based", but I have tried "text/plain" and "text/html" with no luck.
The work-around that I have come up with is to put the response into a file using the "Output File" field of the post task. (Leave "Content Type" blank or this won't work.) You can then read the file into a variable and do what you need to.
This is either a defect in Tasker, incorrect documentation, or just a misunderstanding of what it takes to make it work. This work-around, however, will get you what you need.
Here is a link to a GitHub repository with the updated task export. You may need to change the output file name/location to work with your device.
Update:
Since I posted the above solution, I have run across a Tasker plugin called "RESTask for Tasker". Evidently, Tasker has enough issues with HTML requests that a separate plugin was needed. I have tested this plugin with POST and it does work, so this is another way to go. The plugin is available on the Google Play store.

Create a middle BackEnd for a Mobile app

I am developing a mobile app on Android.
I download a lot of information from the backend via REST API.
For example for obtain the information about a contract I use the api the following request:
GET /contracts/01212314.json
It return a json with many fields
{
"conto_contrattuale": "01212314",
"intestatario": "Dennis D'Amico",
"utilizzo": "COTTURA + PROD. ACQUA 7 GG",
"codice_settore_merceologico": "E1",
"settore_merceologico": "ELETTRICITA",
"codice_societa_vendita": "Z016",
"societa_vendita": "Estra Energie S.r.l.",
"fornitura_indirizzo": "Via Palermo",
"fornitura_civico": "20",
"fornitura_precisazione": "Rosso",
"fornitura_cap": "59100",
"fornitura_comune": "Prato"
"rid": false,
"fatt_elettronica": true,
"fatt_email": "andrea.bettarini#devise.it",
"fatture_scadute": 1,
}
But I am only interested to the field : "fornitura_indirizzo"
I can't modify the backend and the API.
So I think to create a new middle backend that fetch the info from the actual backend and exposes a call only for the field "fornitura_indirizzo".
How can I do?
Is it possible on google cloud platform? and is it free?
Thank you for your consideration.
You can put a file on your server that fetches the data en then recreates a partial array and echo that in json.
Example:
$contract = Json_decode("/contracts/xxxx.json", true);
$needed_info = $contract["fornitura_indirizzo"];
Echo json_encode($needed_info);
I can't access to the server and modify. I can only send request.
I apologise if I misunderstand your goals here, but why bother with creating this new back end for your app. You could simply call the existing back end and ignore the unneeded information. Either way, that existing back end has to send all that info somewhere for every request your mobile app performs. You're also adding more latency to the request since it has to go through your back end first.
I can see this being worth it though if your goal is to minimize the data downloaded by the mobile app when it does a request.

Rest client logging for AndroidAnnotations

I am attempting to use AndroidAnnotation's rest client to access a web service. I am receiving the following error:
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Unexpected character ('f' (code 102)):
was expecting double-quote to start field name
How can I make the rest client log the actual response it received? I can't imagine why my web service is returning this response, but I can't debug it unless I can see the full response. Do I have to set some kind of option at the level of the Spring framework?
I would also like to see the body of the request I am sending.
Thanks for your help!
Here we see that AndroidAnnotations is a wrapper around the Spring Android RestTemplate Module. The code for the RestTemplate is here. So we can find out which TAG is used for logging:
private static final String TAG = "RestTemplate";
Are you not able to see log entries for this TAG? Which converter / extractor are you using? Please post the call stack.
In the wiki they recommend to use a Interceptor for logging request / response. So you could implement your own interceptor like:
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
#Override
public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
logRequest(request);
ClientHttpResponse response = execution.execute(request, data);
logResponse(response);
return response;
}
private void logRequest(HttpRequest request) {
// log it
}
private void logResponse(ClientHttpResponse response) {
// log it
}
}
You enable the interceptor in the #Rest annotation (field interceptors).
While I do not use AndroidAnnotations and cannot answer your question directly, I would like to propose an alternative solution. You could use a great little utility program called Fiddler. It can do wonders for debugging networking activity, whether it be requests, responses, HTTP headers or practically anything else that would matter in a REST API communication.
You can find a full tutorial on how to setup your environment to use Fiddler here, but to name a few crucial steps (credit goes to the linked page, you can also find helpful pictures there)
Setup Fiddler:
Click menu Tools | Fiddler Options, then select the Connections tab
Make note of the “Fiddler listens on” port (normally it’s 8888)
Make sure the check box for “Allow remote computer to connect” is checked
Switch to the HTTPS tab
Make sure the check boxes for “Capture HTTPS Connects” and “Decrypt HTTPS traffic” are both checked
Restart Fiddler
Make note of the PC’s IP address Close non essential apps on the Windows PC (to minimize web traffic being routed through Fiddler)
Setup your device:
Tap on Settings, then Wi-Fi
Find the network on which you’re connected (normally the first one listed), then tap and hold
Choose Modify network from the pop-up
Scroll down and enable “Show advanced options”
Change “Proxy settings” to Manual
Under “Proxy host name” enter the Windows PC IP address from above
Under “Proxy port” enter the Fiddler port from above (usually 8888)
Tap Save and wait a moment for the network to reconnect
Now you will see all the needed details for your REST API calls which makes debugging much easier.

What is Apache-HttpClient/UNAVAILABLE error (Android, Google App Engine)?

I am confronting myself with a problem that I do not know how to interpret. I am doing a project using Android and Google App Engine. When I am trying to save information in Google App Engine's Big Table, directly from the servlet (hardcoded) - the save is performed with no problem. But when I am trying to save data from my Android device, the save is not performed and the log indicates Apache-HttpClient/UNAVAILABLE error. To be more specific:
405 55ms 0kb Apache-HttpClient/UNAVAILABLE (java 1.4)
82.155.246.249 - - [10/Jun/2013:05:20:59 -0700] "POST /servletnamehere HTTP/1.1" 405 306 - "Apache-HttpClient/UNAVAILABLE (java 1.4)" "appnamehere.appspot.com" ms=56 cpu_ms=21 cpm_usd=0.000034 app_engine_release=1.8.0 instance=00c61b117cede3f754aa1ece730dc88287a20199
I have seen that 405 HTTP error appears in the context of a POST method ( "405 errors often arise with the POST method. You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form." ) => indeed, I am trying to perform add (the object is a JPA Entity) in the database using a POST method. The data I am receiving from my Android device is serialized, through an input stream (in my case, working with JSON is not an option, these are the specifications).
Also, 306 HTTP error reffers to switch proxy. While the porevious error might be a bit intuitive, this one is beyond my student knowledge.
I have followed the instructions of this tutorial (http://trumpy.cs.elon.edu/joel/sigcse2011/), which is indeed what I need, but I really cannot figure out what I did wrong. I took again the procedure, stept by step, but I don't see the flaw.
Any help will be indeed appreciated. If I should post some code, I'll gladly do it.
Best wishes,
Cropcircles
LATER EDIT:
Now I get 417 HTTP error expectation failed. I have seen that I am supposed to set a certain parameter on false, but this workaround was available only for .NET. Is there anyone who knows what's the correspondent of the following, in Java? I am really really confused.
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
This is not an answer, but is too long for a comment and may be helpful, so here goes...
First, it's hard to tell what's going on here because there is limited info. Try to post more of the logging either on the client or on the server. Go to the server console and get more info, for instance, or add debugging in the client. I'd start by trying to make the POST from a debug/testing tool like hurl.it: http://www.hurl.it/ (hurl is a nice web front end to a command line HTTP client named curl, see curl itself if you want a more advanced peek). That way you can test and poke around and make SURE your server side works as you expect with a generic client. Then build other clients (like Android).
Second, "Apache-HttpClient/UNAVAILABLE" is not an error, it's just the user-agent String -- so ignore that part. (UNAVAILABLE is where the version typically is, but some impls don't have access to the version sometimes, it seems.)
Third, the 405 response code IS an error, it means POST is not allowed at the server you are trying to POST to. That can either be because POST is not allowed at all on said server, or you're violating some security policy (such as same origin).
If it's AppEngine, first check the APP you are using and make sure it supports POST (look for info on how to do POST at AppEngine to solve, for instance: google app engine: 405 method not allowed). When you say you can do it directly from a "servlet," do you mean that a POST from a different client works? (Servlet is a server side technology, so that's a little confusing.) If so then make sure your Android app is doing the client part the same way to the same host (multipart vs urlencoded, etc).
For a little more on the security stuff, which could be involved, see this post: Google App Engine + jQuery Ajax = 405 Method Not Allowed.
I've had the same trouble and in my case It was an error due to no write permission on server side area.
I was following an android test to write on a file a value transmitted in async way via POST method.
Apache received the POST request but was not able to write the data on the file due to security permission on it.

Categories

Resources