pass data flutter/dart get method - flutter

On the new version of the dart http module for flutter I have a problem
indeed I want to use an API with the get method by passing arguments
'http://host.com?data1=1&data2=2' ...
And since the new version we have to put the url this way
http.get(
Uri.http ('host.com', '/'),
)
or I don't know how to pass the data

Use the query parameters parameter of the Uri.http constructor.
Uri.http ('host.com', '/', {
'data1': '1',
'data2': '2',
...
})

You have to put it in the form
Uri.http ('host.com', '/',parameters),
Inside parameters you have to put a Map like
Map<String, String> parameters = { "data1" : "1" }
And if you are using http dont forget to put
<application android:usesCleartextTraffic="true"/>
inside your AndroidManifest.xml because otherwise you get the next error :) .

For url parameters, you can try just:
String url = 'http://host.com';
url += '?data1=1';
var response = await http.get(url);

You can simply wrap your old url String with Uri.parse.
http.get(Uri.parse('http://host.com?data1=1&data2=2'));

Related

The stock_status parameter does not work in API Woocommerce from flutter

I'm working with flutter and woocommerce_api: ^0.1.0 .
I'm trying to get all the products in a category that have stock_status=instock but that parameter doesn't work.
The same api consumed from PHP does work, so something in flutter is wrong. I've tried all the ways but I can't get it to work.
If I add the parameter ?category=113 , it does work so some parameters are accepted, but if I add the stock_status not working.
I have hidden the keys for security reasons
Any help?
Future getProduct(int idCategory) async {
WooCommerceAPI wooCommerceAPI = WooCommerceAPI(
url: "x",
consumerKey: "x",
consumerSecret: "x");
var productos = await wooCommerceAPI
.getAsync("products?stock_status='instock'&per_page=100");
return productos;
}
Try without quotes.
var productos = await wooCommerceAPI.getAsync("products?stock_status=instock&per_page=100");

http.post problem after Flutter 2 upgrade

I have just updated to Flutter 2. I updated all the dependencies including http. Now I have a problem with the following:
Future<UserLogin> fetchStaff(String pUserName, String pPassword) async {
final response = await http
.post(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'),
headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
body: '{ "pUser": "$pUserName", "pPassword": "$pPassword"}')
.timeout(Duration(seconds: kTimeOutDuration));
I'm getting an error on the: Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'
"The argument type 'String' can't be assigned to the parameter type 'Uri'."
$kBaseUrl = 'https://subdom.mydomain.com:443/mobile';
What do I need to change?
Post method used to take string previously, now they have modified to Uri
so used, Uri.parse to get the uri
.post(Uri.parse(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'))
Like the error says, this is because post function expects a Uri and you have passed a String (returned by Uri.encodeFull) to it. You need to use Uri.parse to pass a Uri to it.
final response = await http
.post(Uri.parse('$kBaseUrl/LoginService/CheckLogin'),
If you had a String, such as:
String strUri="${yourBaseUrl}/yourPath/yourFile";
Using
http.post(Uri.parse(strUri),
headers: {...},
body: {...},
);
is enough to get all working back.
You may try Uri.tryParse(strUri) to handle null if the uri string is not valid as a URI or URI reference.

Getting error for Flutter http.get('url') parameter It's not accepting Url String

I'm trying to fetch all json data which https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail api returns, to use it in my flutter app
var api="https://www.thecocktaildb.com/api/json/v1/1/filter.php?c=Cocktail";
var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];
but it gives red underline under api parameter in http.get(api) function call that "argument of type string cannot be converted to URI"
How to Resolve this? I even tried using
res=await http.get(Uri.https('www.thecocktaildb.com','/api/json/v1/1/filter.php?c=Cocktail'));
but got error "Error connecting to the service protocol: failed to connect to http://127.0.0.1:63573"/_fprblq_tiY=/"
Instead of using Uri.https(), try using Uri.parse()
Add :
http: ^0.13.4 to your pubspec.yaml , it will provide you with some simple methods to access apis.
You cannot add ? directly because it will convert to 3%F I believe, same for other special characters.
var url = 'thecocktaildb.com';
var urlExtension = '/api/json/v1/1/filter.php';
final Map<String, String> queryParameters = <String, String>{
'c': 'Cocktail',
};
final api = Uri.https(url, urlExtension, queryParameters);
//It should be something like this, now you can get
//the api response the way you were doing:
var res=await http.get(api);
drinks= jsonDecode(res.body)["drinks"];

how to get on Dio by passing parameters?

I'm using using Dio 3.0.9, I'm trying to get with parameters, it's returning a 404 error, in Insomnia/Postman it works perfectly ... What is wrong with the code?
Response response = await Dio(
BaseOptions(headers: <String, String>{'authorization': AUTH}))
.get($url, queryParameters: {
"category": {"id": 1}});
note: when using a url without parameters it works, but when it has parameters it doesn't ...
See Dio doesnt supports passing JSON data through it when we are using GET request. So
One option to solve your problem is use http package or do this
Go to Dio.dart and make this small change
if (data != null &&
["POST", "PUT", "PATCH", "DELETE"].contains(options.method)) {
here remove the whole other part that is if (data != null ) {
make it like this.
I have searched about it if you still face difficulty you can go here
github.com/flutterchina/dio/issues/252
Hope This would help you : )

RestAssured response validation using body and array as parameter

I am trying to validate a REST response. Is it possible to use an array as parameter to containsonly?
Ex:
String values[] = line.split(",");
given().
when().
then().
statusCode(200).
body("value", containsOnly(values));
Also, can we use variables as parameters to other methods like HasItems, equalTo etc?
Ex: body(HasItems(values))
Yes, You could use any appropriate matcher to check whole body or just part of it. Just take attention on a object type returned by pecified path - first argument of body().
Try this :
Response resp = RestAssured.given()
.header("Content-Type", "application/vnd.dpd.public.v1+json")
.body(FixtureHelpers.fixture("request/request.json"))
.post("/");
resp
.then()
.statusCode(200)
.body("random.object", CoreMatchers.equalTo("value"));
This would work for request.json object like :
{"random":{"object": "value"}}