Make Go http.Response verbose all parameters - android

I am having a problem getting a parameter sent from android app into go application. I called r.FormValue(key) but it returned null. I want to find the way to check what are parameters available on Go side after the android app sent the post data to it. Is there any way to do this, getting all parameters without using keys?

The Request structure in go has a Form field which is populated with request parameters after ParseForm() is called.
Form contains the parsed form data, including both the URL field's
query parameters and the POST or PUT form data.This field is only
available after ParseForm is called. The HTTP client ignores Form and
uses Body instead.
You could try adding the following code after receiving a request:
func(w http.ResponseWriter, request *http.Request) {
request.ParseForm()
log.Printf("%v",request.Form)
//....
}

If this is for debugging, you can use DumpRequest:
func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
log.Printf("%s", dump)
}

Related

Connection between Android and AWS SageMaker with AWS Lambda

I set up a connection between Android and AWS Lambda which has the endpoint set to SageMaker. I am using the REST API during the connection, the AWS Cognito plug is set to be accessed without authorization.
I make a connection as described here:
https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/
My question is how to send this data:
{"data":"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001"}
And how to view the received response later. Anyone know how to do it or where I can find tips on how to do it?
If I understand correctly, this is your system flow:
POST some data from your Android device
It gets received by API Gateway
And continues through to AWS Lambda
In AWS Lambda the data is extracted, and passed to Sage Maker
Creating a POST using AWS Amplify
To POST data form the Android device, follow the Amplify API (REST) category documentation.
Specifically, you can do something like:
val options = RestOptions.builder()
.addPath("/prod/predictbreastcancer")
.addBody("{\"data\":\"13.49,22.3,86.91,561.0,0.08752,0.07697999999999999,0.047510000000000004,0.033839999999999995,0.1809,0.057179999999999995,0.2338,1.3530000000000002,1.735,20.2,0.004455,0.013819999999999999,0.02095,0.01184,0.01641,0.001956,15.15,31.82,99.0,698.8,0.1162,0.1711,0.2282,0.1282,0.2871,0.06917000000000001\"}".toByteArray())
.build()
Amplify.API.post(options,
{ Log.i("Demo", "POST response = $it") },
{ Log.e("Demo", "POST failed", it) }
)
Creating POST body from EditText content
You mentioned you're using an EditText widget to gather the input data. I assume a user can enter a comma-separated list of values like 0.44, 6.11, etc.
To extract it's content and build the POST body, you can do:
val input = findViewById(R.id.input) as EditText
val body = JSONObject()
.put("data", input.text)
.toString()
.replaceAll("\"", "\\\"")
Displaying response in another Activity
Skimming the blog you referenced, I can't see an example of the response body content. But, here's how you can retrieve response JSON and pass it to a new activity.
Amplify.API.post(options,
{ response ->
val intent = Intent(this, YourOtherActivity::class.java)
intent.putExtra("json", response.data.asString())
runOnUiThread { startActivity(intent) }
},
{ /* handle error ... */ }
)
In YourOtherActivity, access the extra data in onCreate() like so:
val json = intent.getStringExtra("json")

How to get response data on Android using Apollo client for GraphQL if http code is not 200?

I'm new to GraphQL using the Apollo client on Android. I'm trying to sign up a user on the server via graphQL mutation.
I call a mutation and if user's data is not valid I get http code 422: Unprocessible Entity, and in this case I cannot get any response details. I use standard way to make a call:
.enqueue(object : ApolloCall.Callback<CreateUserMutation.Data>() {
override fun onFailure(e: ApolloException) {
}
override fun onResponse(response:
Response<CreateUserMutation.Data>) {
}
And after this I'm in the onFailure block. ApolloException contains only http code description. But I want to see error details in a full response, that I can see via the Postman. I've already tried com.apollographql.apollo.ApolloCall.Callback#onHttpError callback, but the networkResponse body = null. I've described this problem here, but haven't fixed my problem:
https://github.com/apollographql/apollo-android/issues/1166
The way to do this is to check if ApolloException is an instanceof ApolloHttpException.
If it is, you can get the throwable.rawResponse().code() from it.
If it's not it means it isn't server error (maybe timeout or others)

Send data via Http to a server and show response in a Webview

I have an Odoo server that is a WebApp with a website functionality.
I need to implement an Android/iOS app that comunicate with this website functionality.
The website functionality is simple:
Take the intervention code.
check if the state of the intervention sheet is visible into website.
if yes, edit the intervention sheet, if no show error message.
So I want to take the intervention number from Android (for example) and send it by HTTP request and if I get a yes response continue the editing and other stuff in a Webview....if I get "error" show the error into Android Activity.
This is my controller on server that check Code:
#http.route(['/checkCodeAction'],
type='http',
auth="public",
methods=['POST', 'GET'],
csrf=True,
website=True)
def worksheet_code_details(self, **post):
worksheet = request.env['project.task.worksheet']\
.sudo()\
.search([('intervention_number',
'=',
post.get('intervention_number'))])
if worksheet and worksheet.state_id.is_visible_on_frontend:
return redirect(f'/worksheetReadValues/{worksheet.id}')
return request.render(
"website_project_task_worksheet.worksheet_code",
{'error_code': True}
)
The request.render load an xml template of Odoo.... I can intercept this call into a webview?
Or I need to implements another controller for Android that receive for example two response (error, url_with_worksheetid)... so if I get error I show a message, if a get an URL I call the webview with this URL.
I think it's better to return the error using an HTTP Error Status Code and you would be able to better catch that error status code in your Android or IOS controller.
In Odoo you could return a raw werkzeug response like this (the following example from an existing Android-IOS-Odoo integration done by me):
return werkzeug.wrappers.Response(body, status=403, headers=[
('Content-Type', 'application/json'), ('Content-Length', len(body))
])
or you could add an status argument to your already returned render call that it's already a lazy render response wrapper, like:
return request.render(
"website_project_task_worksheet.worksheet_code",
{'error_code': True}, status=403
)

Twilio Status callback url

I am working on voice calling app which is built in java and I need to know the call status when it is picked , rejected or complated.My server end is in java.
I set status callback url while placing a call as mention in the twilio docs. My question which url is to added in that code and do i need to add the funtion for that end point url also.
And what should be the code in that funtion like what are the parameters as I need to print the call status
com.twilio.type.Client clientEndpoint = new com.twilio.type.Client("client:" + to);
PhoneNumber from = new PhoneNumber(CALLER_ID);
// Make the call
Call call = Call.creator(clientEndpoint, from, uri).setMethod(HttpMethod.POST)
.setStatusCallbackMethod(HttpMethod.POST)
.setStatusCallback(URI.create("https://57fb8b2c.ngrok.io/events"))
.setStatusCallbackEvent(
Arrays.asList(Call.Event.ANSWERED.toString(), Call.Event.COMPLETED.toString(),
Call.Event.INITIATED.toString(), Call.Event.RINGING.toString()))
.create(client);
// Print the call SID (a 32 digit hex like CA123..)
System.out.println(call.getSid() + "//" + call.getStatus());
return call.getSid();
Twilio developer evangelist here.
I'm not particularly good at Java, but I can help with what happens when you set a statusCallback URL.
For each of the events you set as the statusCallbackEvent you will receive an HTTP request to your statusCallback URL when the call enters that state.
You will need to implement an endpoint (in your case, at the path /events as that's the URL you are setting) that can receive these incoming HTTP requests.
When Twilio makes the status callback request it includes all the regular webhook parameters, such as CallSid so you can tie the request to the known call sid.
The request will also include some other parameters, most importantly in your case the CallStatus parameter. The value will be one of queued, initiated, ringing, in-progress, busy, failed, or no-answer. There's more on what they mean here.
I hope that helps a bit.

getEntity call results in crash (using odata4j on a WCF service)

I am trying out odata4j in my android app to retrieve data from a DB that can be accessed from a WCF service.
ODataConsumer co = ODataConsumer.create("http://xxx.xx.xx.xxx:xxxx/Users");
for(OEntity user : co.getEntities("Users").execute())
{
// do stuff
}
However this crashes at the call to getEntities. I have tried a variety of other calls as well, such as
Enumerable<OEntity> eo = co.getEntities("Users").execute();
OEntity users = eo.elementAt(0);
However this also crashes at eo.elementAt(0).
The logcat doesn't tell me anything, and the callstack seems to be Suspended at ActivityThread.performLaunchActivity.
Entering "http://localhost:xxxx/Users" in my web browser on the other hand works as expected and returns the users in my DB in xml format.
Any ideas on how I can debug this?
To log all http requests/responses:
ODataConsumer.dump.all(true);
The uri passed to the consumer .create call should be the service root. e.g. .create("http://xxx.xx.xx.xxx:xxxx/"); Otherwise your code looks fine.
Note the Enumerable behaves like the .net type - enumeration is deferred until access. If you plan on indexing multiple times into the results, I'd suggest you call .toList() first.
Let me know what you find out.
Hope that helps,
- john
I guess the call should be:
ODataConsumer co = ODataConsumer.create("http://xxx.xx.xx.xxx:xxxx");
for(OEntity user : co.getEntities("Users").execute())
{
// do stuff
}
create defines service you want to connect but Users is the resource you want to query.
Can you try this way.
OEntity oEntity;
OQueryRequest<OEntity> oQueryRequest= oDataJerseyConsumer.getEntities(entityName);
List<OEntity> list= oQueryRequest.execute().toList();
for (OEntity o : list) {
List<OProperty<?>> props = o.getProperties();
for (OProperty<?> prop : props) {
System.out.println(prop.getValue().toString());
}
}

Categories

Resources