I would like to send a synchronous POST request from the client-side. According to the documentation we can use the 'async' named parameter:
https://www.dartlang.org/articles/json-web-service/#saving-objects-on-the-server
var url = "http://127.0.0.1:8080/programming-languages";
request.open("POST", url, async: false);
But the above example throws the following syntax error:
The keywords 'async', 'await', and 'yield' may not be used as identifiers in an asynchronous or generator function.
How can I send a synchronous POST request?
UPDATE (27 May, 20:23)
I found a workaround to solve this problem:
Future<String> deleteItem(String id) async {
final req = new HttpRequest()
..open('POST', 'server/controller.php')
..send({'action': 'delete', 'id': id});
// wait until the request have been completed
await req.onLoadEnd.first;
// oh yes
return req.responseText;
}
But I don't like the above solution because it doesn't seem elegant enough.
This is a known issue with this named parameter https://github.com/dart-lang/sdk/issues/24637
The solution is to use postFormData() instead of send(). For example:
final req = await HttpRequest
.postFormData(url, {'action': 'delete', 'id': id});
return req.responseText;
Future<String> deleteItem(String id) async {
final req = new HttpRequest()
..open('POST', 'server/controller.php')
..send({'action': 'delete', 'id': id});
// wait until the request have been completed
await req.onLoadEnd.first;
// oh yes
return req.responseText;
}
this one is the point, where sometimes you need "PUT" or "DELETE"
Related
I'm using the Dart Shelf package and I need to log the response it sends.
I've managed to log the request but the response technique is less clear:
final handler = const shelf.Pipeline()
.addMiddleware(corsHeaders())
.addMiddleware(shelf.logRequests(
logger: (message, isError) =>
_logRequest(message, isError: isError)))
.addHandler((req) async {
final res = await Router().call(req);
return res;
});
There two parts to the question.
how do I log the headers.
is it possible to log the body.
I know there is an issue in that the response body can only be read once.
As some of the responses are likely to be large I need to filter the requests for which the body is logged.
The answer is a bit of Dart-fu. You have an anonymous function returning an anonymous function.
var handler = const Pipeline()
.addMiddleware(
(handler) => (request) async {
final response = await handler(request);
print(response.headers);
// you could read the body here, but you'd also need to
// save the content and pipe it into a new response instance
return response;
},
)
.addHandler(syncHandler);
I need track statusCallbackEvent when call answered, completed, also need to pass StatusCallback with custom parameter please suggest how to achieve it?
my code is something like
public CallResource MakeOutboundPhoneCallsAsync(OutgoingCallRequest request, string accountSid, string authToken)
{
try
{
TwilioClient.Init(accountSid, authToken);
List<string> statusCallbackEvent = new List<string> { "answered", "completed" };
CallResource response =
CallResource.Create(
url: new Uri("http://demo.twilio.com/docs/voice.xml"),
to: new PhoneNumber(request.ToPhoneNumber),
from: new PhoneNumber(request.FromPhoneNumber),
method: Twilio.Http.HttpMethod.Get,
statusCallback: new Uri("http://b28deaf4.ngrok.io/api/Twilio/OutboundCalls/StatusCallback"),
statusCallbackEvent: statusCallbackEvent,
statusCallbackMethod: Twilio.Http.HttpMethod.Post
);
return response;
}
catch (Exception e)
{
throw e;
}
}
Twilio developer evangelist here.
If you want to pass more parameters back to your application via the webhook, you can do so by including query parameters in the URL. For example:
statusCallback: new Uri("http://b28deaf4.ngrok.io/api/Twilio/OutboundCalls/StatusCallback?ExtraParameter=InTheQueryString"),
Then you can read them out of the URL when your application receives the request.
I made a function to post notification to a topic. It works great in normally, then I put it in compute function and hope it can posts notification in the background. But it not works.
Here is my code:
void onSendMessageInBackGround(String message) {
Future.delayed(Duration(milliseconds: 3000)).then((_) async{
Client client = Client();
final requestHeader = {'Authorization': 'key=my_server_key', 'Content-Type': 'application/json'};
var data = json.encode({
'notification': {
'body': 'tester',
'title': '$message',
},
'priority': 'high',
'data': {
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'dataMessage': 'test',
'time': "${DateTime.now()}",
},
'to': '/topics/uat'
});
await client.post('https://fcm.googleapis.com/fcm/send', headers: requestHeader, body: data);
});
}
call compute:
compute(onSendMessageInBackGround, 'abc');
Note: I has put the onSendMessageInBackGround function at the top level of my app as the library said
Is it missing something? or we can't do that?
You might need to add a return or await
void onSendMessageInBackGround(String message) {
return /* await (with async above) */ Future.delayed(Duration(milliseconds: 3000)).then((_) async{
It could be that the isolate shuts down before the request is made because you're not awaiting the Future
Function called from compute must be static or global.
Either i agree with pskink, compute here is not usefull.
Isolates communicate by passing messages back and forth. These messages can be primitive values, such as null, num, bool, double, or String, or simple objects such as the List in this example.
You might experience errors if you try to pass more complex objects, such as a Future or http.Response between isolates.
Got this from the documentation here
I tried to change the existing angularjs library to angular2 for my need. http.post method in the below code throws TypeError {} as exception. Someone please help as i am stuck on this.
login() {
return new Promise((resolve, reject) => {
if(typeof jsSHA !== "undefined") {
var signatureObj = (new OauthUtility()).createSignature("POST", this.magentoOptions.baseUrl+"/oauth/initiate", this.oauthObject, {oauth_callback: "http://localhost/callback"}, this.magentoOptions.clientSecret, null);
let headersInitiate = new Headers();
headersInitiate.append('Authorization',signatureObj.authorization_header);
headersInitiate.append('Content-Type','application/x-www-form-urlencoded');
let url = this.magentoOptions.baseUrl + "/oauth/initiate";
let callback = "oauth_callback=http://localhost/callback";
try{
this.http.post(url, callback,{headers: headersInitiate})
.subscribe(
(result) => {
console.log("i am inside");
var rParameters = (result).split("&");
.....
}
catch(Exception){
console.log(Exception)
}
You should try something like that:
var signatureObj = (new OauthUtility()).createSignature("POST",
this.magentoOptions.baseUrl+"/oauth/initiate", this.oauthObject,
{oauth_callback: "http://localhost/callback"},
this.magentoOptions.clientSecret, null); let headersInitiate = new Headers();
headersInitiate.append('Authorization',
signatureObj.authorization_header);
headersInitiate.append('Content-Type',
'application/x-www-form-urlencoded');
let url = this.magentoOptions.baseUrl + "/oauth/initiate";
let payload = ' ... ';
this.http.post(url, payload,{headers: headersInitiate})
.subscribe(
(result) => {
console.log("i am inside");
var rParameters = (result).split("&");
(...)
});
Here are the comments I would have on your code:
The second parameter of the post method should be a string corresponding to the payload not a callback. I see from your headers that you want to send url-encoded form, so you need to create it by your own
The try catch isn't necessary since executing an HTTP is asynchronous and errors can be "catched" within the second parameter (another callback) of the subscribe method.
You don't need at all a promise. For HTTP, Angular2 uses observables under the hood. They target asynchronous processing as well.
After fixing all of this, I think that you won't have error anymore...
Hope it helps you,
Thierry
I found to stuck even after proceeding with the above all steps. The complete solution is as follows.
Remove try catch block and promise as suggested by Thierry.
Use dependency injection of http inside the constructor as follows to define http.
import {Http,HTTP_PROVIDERS,Headers} from 'angular2/http';
import {Injector} from "angular2/core";
constructor() {
var injector = Injector.resolveAndCreate([HTTP_PROVIDERS]);
this.http = injector.get(Http);
}
Here is a problem. When I ran these code:
String responseText = null;
HttpRequest.getString(url).then((resp) {
responseText = resp;
print(responseText);
});
print(responseText);
In console:
{"meta":{"code":200},"data":{"username":"kevin","bio":"CEO \u0026 Co-founder of Instagram","website":"","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg","full_name":"Kevin Systrom","counts":{"media":1349,"followed_by":1110365,"follows":555},"id":"3"}}
null
It running asynchronously. Is there JAVA way with synchronized method? That will be await while request is done?
I found only one tricky way to do it and its funny -- wait for three seconds:
handleTimeout() {
print(responseText);
}
const TIMEOUT = const Duration(seconds: 3);
new Timer(TIMEOUT, handleTimeout);
And of course it works with bugs. So any suggestions?
MattB way work well:
var req = new HttpRequest();
req.onLoad.listen((e) {
responseText = req.responseText;
print(responseText);
});
req.open('GET', url, async: false);
req.send();
First, I'm assuming you're using this as a client-side script and not server-side. Using HttpRequest.getString will strictly return a Future (async method).
If you absolutely must have a synchronous request, you can construct a new HttpRequest object and call the open method passing the named parameter: async: false
var req = new HttpRequest();
req.onLoad.listen((e) => print(req.responseText));
req.open('GET', url, async: false);
req.send();
However it is highly recommended that you use async methods for accessing network resources as a synchronous call like above will cause the script to block and can potentially make it appear as though your page/script has stopped responding on poor network connections.