I am using rest API in my flutter app. For further request I need JSESSIONID which I received from my profile API. I successful got response but I need guide to iterate cookie value.
I followed following steps:
final response = await http.get(
strURL,
headers: {
"Authorization": basicAuth,
"Content-Type": "application/json"
},
);
String rawCookie = response.headers['set-cookie'];
print('rawCookie $rawCookie');
As print raw cookie it is printing details:
flutter: rawCookie __cfduid=d5bbe3f8a131478a78ae996e636cca0401544177738; expires=Sat, 07-Dec-19 10:15:38 GMT; path=/; domain=.rayz.ch; HttpOnly,JSESSIONID=6AD6698C5BFC90F1D089696A955E6824; Path=/; HttpOnly
I can iterate it by substring but I want to iterate it with a proper way. So please guide me on this.
With package:http you need to split the cookie string yourself using String.split. If you want to use the underlying http client, that gives you a pre-parsed list of cookies, for example:
HttpClient _httpClient = new HttpClient();
HttpClientRequest request = await _httpClient.postUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
print(response.cookies); // this is a List<Cookie>
Here is my code which runs perfectly and if any key does not have value, then it shows range error. But as your doubt, this code is running fine.
var headersList = response.headers['set-cookie']!.split(";");
for(var kvPair in headersList){
var kv = kvPair.split("=");
var key = kv[0];
var value = kv[1];
if(key.contains("session_id")){
print(value);
}
}
Related
I am making POST request using http lib in Dart. After I wandering around former answers with no luck. Even official doc
import 'package:http/http.dart' as http;
Future<http.Response> createAlbum(String nameEN) {
return http.post(
'http://localhost:8000/api/products/',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'nameEN': nameEN,
}),
);
}
And I test with unittest like this
testWidgets('POST request from offcial docs', (WidgetTester tester) async{
final http.Response res = await createAlbum("Jordan");
print(res.statusCode);
});
Attempt:
PostmanApp can make request and get 201 in response. However, when I copy&paste the Dart code from it and test it does not work
I always get 400 and no request comes to localhost:8000
testWidgets('POST request from postman', (WidgetTester tester) async{
var headers = {
'Content-Type': 'application/json'
};
var request = http.Request('POST', Uri.parse('localhost:8000/api/products/'));
request.body = '''{\n "name_jp": "李天宝",\n "name_en": "Sarit",\n "description": "Developer",\n "qty": 3,\n "expiry": "2099-12-25",\n "barcode": "549XXXYYYYYY",\n "price": "{\\"bounds\\": \\"[)\\", \\"lower\\": \\"12\\", \\"upper\\": \\"6\\"}",\n "medium_price": 60\n}''';
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(200);
print(await response.stream.bytesToString());
}
else {
print(response.statusCode);
print(response.reasonPhrase);
}
});
Problem
Dart response returned to me is 400 and on the server side request has not been sent out to my http://localhost:8000/api/products/
Question:
What is the correct POST syntax?
References:
HTTP POST with Json on Body - Flutter/Dart
Bad state: Cannot set the body fields of a Request with content-type "application/json"
Flutter FormatException: Unexpected character (at character 1)
Http POST request with json content-type in dart:io
You have non-ASCII characters. Did you set the proper ; charset=UTF-8 in your Content-Type header? Or even better, JSON doesn't understand non-ASCII unless you \u encode them.
Lesson learnt here
Add CORS enable in chrome
flutter run -d web. Do not use -d chrome
Make request from cors enabled chrome by clicking. Do not use unittest!
I am using this code to send http request using Dart:
var request = await httpClient.postUrl(Uri.parse(url));
Map jsonMap = {'pageSize': 10};
request.add(utf8.encode(json.encode(jsonMap)));
request.headers.set('content-type', 'application/json');
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
}
but it shows:
HttpException: HTTP headers are not mutable
why give me this tips and what should I do fix this?
You need to check HttpHeaders class page. (https://api.dart.dev/stable/2.10.5/dart-io/HttpHeaders-class.html)
HttpRequest and HttpClientResponse always have immutable headers.
HttpResponse and HttpClientRequest have immutable headers from the moment the body is written to.
request.add(utf8.encode(json.encode(jsonMap)));
This line is add Request Body so made HttpClientRequest header to immutable. So if you want to add headers, you need to do it before this line.
var request = await httpClient.postUrl(Uri.parse(url));
Map jsonMap = {'pageSize': 10};
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
}
I am trying to connect with our server using dart and flutter. I get an error in certificate server, I get the code and I get response exactly, but the problem is the response keeps coming back as a string. I want it as a list to loop through it.
HttpClient client = new HttpClient();
client.badCertificateCallback =((X509Certificate cert, String host, int port) => true);
String url ='https://xxx';
//Map map = { "email" : "email" , "password" : "password"};
HttpClientRequest request = await client.getUrl(Uri.parse(url));
//request.headers.set('content-type', 'application/json');
//request.add(utf8.encode(json.encode(map)));
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
print(reply);
I have simple code to get a JSON response as a list. The problem is our server is using https and nginx to take all request to the correct port. Previous code worked but I need to respond with a list.
simple code is :
String apiURL = "https://jsonplaceholder.typicode.com/posts";
http.Response response = await http.get(apiURL);
return json.decode(response.body);
You won't get a list. You would get flat json which you would need to process. Processing can be done using simple script using powershell.
I am trying to get data from API. I need to pass value from the body, in postman without a header: application/JSON data is not displayed.
final response = await http.post(
"http://192.168.10.25:8080/Login/validateusername",
body: {"username": "user#PYA"},
headers: {'Content-Type': 'application/json'},
);
Error Message:
E/flutter (28851): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
E/flutter (28851): Bad state: Cannot set the body fields of a Request with content-type "application/json".
Add the content type application/json
Future<String> apiRequest(String url, Map jsonMap) async {
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
// todo - you should check the response.statusCode
String reply = await response.transform(utf8.decoder).join();
httpClient.close();
return reply;
}
Simply encode body to json object when using content-type "application/json"
http.Response response = await http.post( uri , headers: headers, body: JsonEncoder().convert(body));
Another simple way is as bellow
import 'package:http/http.dart' as http;
String body = json.encode({
'foo': 'bar',
'complex_foo' : {
'name' : 'test'
}
});
http.Response response = await http.post(
url: 'https://example.com',
headers: {"Content-Type": "application/json"},
body: body,
);
use the http dart package
var data = {username:"username",password:"password"};
http.Response response = await http.post(
"yourApiroute",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: {"username": data.phone, "password": data.password});
var json = jsonCodec.encode(data);
print("json=$json");
var url = "http:yourAPIrouter.com/etc";
var response = await http.post(
url,
headers:{ "Accept": "application/json" } ,
body: { "json": '$json'},
encoding: Encoding.getByName("utf-8")
);
and dont forget add the key "json" in postman
I am doing almost the same. However, I tried to avoid doing back-end, like in your case. I just did a minimal php request so that I would not waste or patience learning what is needed to develop a user management controller.
However, I faced several limitations and problems that Flutter alone can't solve. After some denial, I gave a try. Lumen, a light version of the Laravel Framework, some tutorials and some past experience, I eventually realized that the API should carry most of the authentication, and not the application itself. I digressed.
In my case, the code of the fuction to a http post is:
Future<Post> createPost() async {
final url = "http://localhost:8000/api/login";
Map<String, String> body = {
'user': user.text,
'pass': pass.text,
};
await http.post(url, body: body);
print(body);
return http.;
}
I first convert it into a map. I prefer this method over parsing json, because down the line, if I need to add more variables, I just make the map bigger.
I just have a question: What does your http://192.168.10.25:8080/Login/validateusername look like? I think that there is no need to specify the type of information that your body parses.
HttpClient myClient = new HttpClient();
myClient.BaseAddress = new Uri(URL);
Base address already specified on client it's URL.
var encodedObject = JsonConvert.SerializeObject(Obj);
myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await myClient.PostAsync("test.php/yourAPI",new StringContent(encodedObject, System.Text.Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
var responseContent = response.ToString();
var responsebody = response.Content.ToString();
Stream receiveStream = response.GetResponseStream();
string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
}
Could be a content negotiation issue. Try clearing the Accept header before adding the json media type
myClient.DefaultRequestHeaders.Accept.Clear();
myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//...other code removed for brevity.
this code sets the Accept header to "application/json", which tells the server to send data in JSON format.
Reference source: Calling a Web API From a .NET Client in ASP.NET Web API 2
the Method PostAsync has as first argument the complete URI of The API. Therefore, it should be like follow :
HttpResponseMessage response = await myClient.PostAsync("http://bla-bla-bla/test.php/test",new StringContent(encodedObject, System.Text.Encoding.UTF8, "application/json"));
And there is no need to define the BaseAddress.