dart: wrap URL in Uri.parse() (#352)

This commit is contained in:
Boris Verkhovskiy
2022-03-18 23:52:44 -07:00
committed by GitHub
parent 8d5700c72b
commit 255a4e9389
24 changed files with 49 additions and 25 deletions

View File

@@ -1,7 +1,8 @@
import 'package:http/http.dart' as http;
void main() async {
var res = await http.delete('http://www.url.com/page');
var url = Uri.parse('http://www.url.com/page');
var res = await http.delete(url);
if (res.statusCode != 200) throw Exception('http.delete error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -6,7 +6,8 @@ void main() async {
var pword = 'some_password';
var authn = 'Basic ' + base64Encode(utf8.encode('$uname:$pword'));
var res = await http.get('https://api.test.com/', headers: {'Authorization': authn});
var url = Uri.parse('https://api.test.com/');
var res = await http.get(url, headers: {'Authorization': authn});
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -6,7 +6,8 @@ void main() async {
var pword = 'some_password';
var authn = 'Basic ' + base64Encode(utf8.encode('$uname:$pword'));
var res = await http.get('https://api.test.com/', headers: {'Authorization': authn});
var url = Uri.parse('https://api.test.com/');
var res = await http.get(url, headers: {'Authorization': authn});
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -14,7 +14,8 @@ void main() async {
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('http://api.ipify.org/?$query', headers: headers);
var url = Uri.parse('http://api.ipify.org/?$query');
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -46,7 +46,8 @@ void main() async {
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('https://www.nomador.com/house-sitting/?$query');
var url = Uri.parse('https://www.nomador.com/house-sitting/?$query');
var res = await http.get(url);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -1,7 +1,8 @@
import 'package:http/http.dart' as http;
void main() async {
var res = await http.get('http://www.google.com');
var url = Uri.parse('http://www.google.com');
var res = await http.get(url);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -8,7 +8,8 @@ void main() async {
var data = '18233982904';
var res = await http.post('http://198.30.191.00:8309/CurlToNode', headers: headers, body: data);
var url = Uri.parse('http://198.30.191.00:8309/CurlToNode');
var res = await http.post(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -12,7 +12,8 @@ void main() async {
'Accept-Encoding': 'gzip',
};
var res = await http.get('http://en.wikipedia.org/', headers: headers);
var url = Uri.parse('http://en.wikipedia.org/');
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -5,7 +5,8 @@ void main() async {
'foo': 'bar',
};
var res = await http.get('http://example.com/', headers: headers);
var url = Uri.parse('http://example.com/');
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -1,7 +1,8 @@
import 'package:http/http.dart' as http;
void main() async {
var res = await http.get('http://indeed.com');
var url = Uri.parse('http://indeed.com');
var res = await http.get(url);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -6,7 +6,8 @@ void main() async {
'Content-Type': 'application/x-www-form-urlencoded',
};
var res = await http.post('http://localhost:28139', headers: headers);
var url = Uri.parse('http://localhost:28139');
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'foo=\"bar\"';
var res = await http.post('http://example.com/', body: data);
var url = Uri.parse('http://example.com/');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'foo=\'bar\'';
var res = await http.post('http://example.com/', body: data);
var url = Uri.parse('http://example.com/');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -14,7 +14,8 @@ void main() async {
'Accept-Encoding': 'gzip',
};
var res = await http.post('http://www.w3schools.com/ajax/demo_post.asp', headers: headers);
var url = Uri.parse('http://www.w3schools.com/ajax/demo_post.asp');
var res = await http.post(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -8,7 +8,8 @@ void main() async {
var data = utf8.encode('{"title":"china1"}');
var res = await http.post('http://localhost:28139/post', headers: headers, body: data);
var url = Uri.parse('http://localhost:28139/post');
var res = await http.post(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'msg1=wow&msg2=such&msg3=@rawmsg';
var res = await http.post('http://example.com/post', body: data);
var url = Uri.parse('http://example.com/post');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'foo="bar"';
var res = await http.post('http://example.com/', body: data);
var url = Uri.parse('http://example.com/');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'foo="bar"';
var res = await http.post('http://example.com/', body: data);
var url = Uri.parse('http://example.com/');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -3,7 +3,8 @@ import 'package:http/http.dart' as http;
void main() async {
var data = 'foo=\'bar\'';
var res = await http.post('http://example.com/', body: data);
var url = Uri.parse('http://example.com/');
var res = await http.post(url, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -16,7 +16,8 @@ void main() async {
var data = 'msg1=wow&msg2=such';
var res = await http.post('http://fiddle.jshell.net/echo/html/', headers: headers, body: data);
var url = Uri.parse('http://fiddle.jshell.net/echo/html/');
var res = await http.post(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -14,7 +14,8 @@ void main() async {
var data = 'CultureId=1&ApplicationId=1&RecordsPerPage=200&MaximumResults=200&PropertyTypeId=300&TransactionTypeId=2&StoreyRange=0-0&BuildingTypeId=1&BedRange=0-0&BathRange=0-0&LongitudeMin=-79.3676805496215&LongitudeMax=-79.27300930023185&LatitudeMin=43.660358732823845&LatitudeMax=43.692390574029936&SortOrder=A&SortBy=1&viewState=m&Longitude=-79.4107246398925&Latitude=43.6552047278685&ZoomLevel=13&CurrentPage=1';
var res = await http.post('http://www.realtor.ca/api/Listing.svc/PropertySearch_Post', headers: headers, body: data);
var url = Uri.parse('http://www.realtor.ca/api/Listing.svc/PropertySearch_Post');
var res = await http.post(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -13,7 +13,8 @@ void main() async {
var data = '{"admins":{"names":[], "roles":[]}, "readers":{"names":["joe"],"roles":[]}}';
var res = await http.put('http://localhost:5984/test/_security', headers: headers, body: data);
var url = Uri.parse('http://localhost:5984/test/_security');
var res = await http.put(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.put error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -7,7 +7,8 @@ void main() async {
var data = '{"properties": {"email": {"type": "keyword"}}}';
var res = await http.put('http://localhost:9200/twitter/_mapping/user?pretty', headers: headers, body: data);
var url = Uri.parse('http://localhost:9200/twitter/_mapping/user?pretty');
var res = await http.put(url, headers: headers, body: data);
if (res.statusCode != 200) throw Exception('http.put error: statusCode= ${res.statusCode}');
print(res.body);
}

View File

@@ -85,10 +85,11 @@ export const _toDart = r => {
}
if (hasQuery) {
s += ' var res = await http.' + r.method + "('" + r.urlWithoutQuery + "?$query'"
s += " var url = Uri.parse('" + r.urlWithoutQuery + "?$query');\n"
} else {
s += ' var res = await http.' + r.method + "('" + r.url + "'"
s += " var url = Uri.parse('" + r.url + "');\n"
}
s += ' var res = await http.' + r.method + '(url'
if (hasHeaders) s += ', headers: headers'
else if (r.auth) s += ", headers: {'Authorization': authn}"