i want to get current weather information of coordinated base from openweathermap API.
For this purpose i am using flutter's HTTP Package.
the problem is here when i use this method
One more problem is that... I got these coordinates on Memu App Player but does not gives on Real android device and Android Studio's Own Emulator.
void getWeatherData()async{
String url = 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33';
Response response = await get(Uri.parse(url));
print(response.body); }
this should give me API result completely but it gives me this error when i run my app.
Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform: http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33
This is how I did it. I'm going through a course in Udemy that is going over what you are asking.
void getData() async {
var url = Uri.https('samples.openweathermap.org', 'data/2.5/weather', {
'lat': '35',
'lon': '139',
'appid': 'b6907d289e10d714a6e88b30761fae22'
});
http.Response response = await http.get(url);
print(weatherResponse.body);
}
Remember to use
import 'package:http/http.dart' as http;
Related
I use packages
http: ^ 0.12.2
this is my iport
import 'package: http / http.dart' as http;
import 'dart: io';
import 'dart: convert';
for retrieving the data from a server. It works fine with Android and iOS. But on the web I get the error message Unsupported operation: Platform._version
this is my function:
Future getUserData() async {
var url_ = "${APIDomain}/antragsteller/1";
HttpClient client = new HttpClient();
client.badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
HttpClientRequest request = await client.getUrl(Uri.parse(url_));
request.headers.set('content-type', 'application/json');
request.headers.set('Access-Control-Allow-Origin', '*');
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
this.userData = jsonDecode(reply);
}
the return value is then of course empty. My internet search did not produce any useful results.
Can someone please help me find the cause and solve the problem. Thank you!
This was caused by an issue on Dart SDK libraries and Flutter for web as documented on this GitHub issue thread. This has been resolved for the http plugin, updating to the latest version of the plugin should solve the issue.
Problem
I was trying to use 'aws-amplify' GET API request with query parameters on the client side, but it turned out to be Request failed with status code 403, and the response showed:
"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
Note: React.js as front-end, Javascript as back-end.
My code
Front-end
function getData() {
const apiName = 'MyApiName';
const path = '/path';
const content = {
body:{
data:'myData',
},
};
return API.get(apiName, path, content);
}
Back-end
try {
const result = await dynamoDbLib.call("query", params);
} catch (e) {
return failure({ status: false });
}
What I did to debug
The GET lambda function works fine in Amazon Console (Tested)
If I change the backend lambda function so that the frontend request can be made without parameters, i.e. return API.get(apiName, path), then no error shows up.
My question
How can I make this GET request with query parameters works?
I changed GET to POST (return API.post()), everything works fine now.
If anyone can provide a more detailed explanation, it would be very helpful.
I have a plot set up to use an AjaxDataSource. This is working pretty well in my local development, and was working as deployed in my Kubernetes cluster. However, after I added HTTPS and Google IAP (Identity-Aware Proxy) to my plotting app, all of the requests to the data-url for my AjaxDataSource are rejected by the Google IAP service.
I have run into this issue in the past with other AJAX requests to Google IAP-protected services, and resolved it by setting {withCredentials: true} in my axios requests. However, I do not have this option while working with Bokeh's AjaxDataSource. How do I get BokehJS to pass the cookies to my service in the AjaxDataSource?
AjaxDataSource can pass headers:
ajax_source.headers = { 'x-my-custom-header': 'some value' }
There's not any way to set cookies (that would be set on the viewer's browser... which does not seem relevant in this context). Doing that would require building a custom extension.
Thanks to bigreddot for pointing me in the right direction. I was able to build a custom extension that did what I needed. Here's the source code for that extension:
from bokeh.models import AjaxDataSource
from bokeh.util.compiler import TypeScript
TS_CODE = """
import {AjaxDataSource} from "models/sources";
export class CredentialedAjaxDataSource extends AjaxDataSource {
prepare_request(): XMLHttpRequest {
const xhr = new XMLHttpRequest();
xhr.open(this.method, this.data_url, true);
xhr.withCredentials = true;
xhr.setRequestHeader("Content-Type", this.content_type);
const http_headers = this.http_headers;
for (const name in http_headers) {
const value = http_headers[name];
xhr.setRequestHeader(name, value)
}
return xhr;
}
}
"""
class CredentialedAjaxDataSource(AjaxDataSource):
__implementation__ = TypeScript(TS_CODE)
Bokeh extensions documentation: https://docs.bokeh.org/en/latest/docs/user_guide/extensions.html
I have noticed a rather strange error with the Emotion API from the Cognitive Services suite.
Everything works just fine as long as I send it URL's. When sending it image attachments. I receive this JSON error:
{ error: { code: 'InvalidImageSize', message: 'Image size is too small or too big.' } }
Sending it smaller or larger versions does not help.
Send a URL of the same image, and suddenly it works fine again.
I stream the attachment to the API service in the exact same way I do for another Cognitive Services API, namely Computer Vision. And that works great with streamed attachments.
The code is on GitHub: https://github.com/sebsylvester/botbuilder-mcs
I know the APIs are still in preview, but this is still a weird issue.
Unfortunately, the Emotion and Face APIs do not support chunked transfers, as noted here. The 'workaround' is to load the image bits synchronously prior to making the web request. The code snippet from that project is thus:
function _postImageSync(url, image, options) {
return new _Promise(function (resolve, reject) {
request.post({
uri: host + rootPath + url,
headers: {
'Ocp-Apim-Subscription-Key': key,
'Content-Type': 'application/octet-stream'
},
qs: options,
body: fs.readFileSync(image)
}, (error, response) => {
response.body = JSON.parse(response.body);
_return(error, response, resolve, reject);
});
});
}
I'm developing a Dart application that will need authentication and session control. I'm trying shelf_auth to do that, but the examples doesn't seem to work or, more likely, I'm not implementing them the right way.
In short, this is what I want to happen:
An user opens the application on the browser.
The user enters the login information (login and password), which are POSTED to the server.
If the provided information is valid, the application generates a session code that is passed to the client and stored on the DB (server-side). This code will be sent with every transaction to the server-side.
The package shelf_auth has some examples, but I don't know which one to follow. So my question is: how could I do that with shelf_auth? I'm not asking for anyone to code this for me, but just to point me to the right direction.
EDIT: The example that I was trying out was this: example_with_login_and_jwt_session.dart. Seems that it's lacking CORS headers (this question helped me fixing it) and, even providing valid information, it responds "Unauthorized".
This is how I'm POSTING the information:
import "dart:html";
void main() {
Map _queryParameters = {
"username": "fred",
"password": "blah"
};
var _button = querySelector("#login_button");
_button.onClick.listen((MouseEvent e) {
e.preventDefault();
var requisition = new HttpRequest();
Uri uri = new Uri(path: "http://localhost:8080/login", queryParameters: _queryParameters);
requisition.open("POST", uri.toString());
requisition.setRequestHeader("content-type", "application/x-www-form-urlencoded");
requisition.onLoadEnd.listen((_) {
print(requisition.response.toString());
});
requisition.send();
});
}
I got it working with this client code
import "dart:html";
void main() {
Map _queryParameters = {"username": "fred", "password": "blah"};
var _button = querySelector("#login_button");
_button.onClick.listen((MouseEvent e) async {
e.preventDefault();
var requisition = new HttpRequest();
Uri uri = new Uri(
path: "http://localhost:8080/login/");
requisition.onLoadEnd.listen((_) {
print(requisition.response.toString());
});
HttpRequest request = await HttpRequest.postFormData(
"http://localhost:8080/login/", _queryParameters
//,withCredentials: true
);
print(request.response);
});
}
The example server expects the credentials in the body instead of query parameters and I set withCredentials: true so authentication cookies are sent with the request. Worked without withCredentials.