Return response body from HttpClient request - dart

Here is my code:
Future<String> fetch(url) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var cj = new PersistCookieJar(tempPath);
HttpClient client = new HttpClient();
var request = await client.getUrl(Uri.parse(url));
request.cookies.addAll(cj.loadForRequest(Uri.parse(url)));
var response = await request.close();
cj.saveFromResponse(Uri.parse(url), response.cookies);
// I tried using .toString() but it returns an " Instance of '_HttpClientResponse' "
return response.toString();
}
I tried for hours to find a way to return the response body, but without any success, could someone please help me.

Never mind, I fixed it.
I used this code:
Future<String> fetch(url) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var cj = new PersistCookieJar(tempPath);
HttpClient client = new HttpClient();
var request = await client.getUrl(Uri.parse(url));
request.cookies.addAll(cj.loadForRequest(Uri.parse(url)));
var response = await request.close();
cj.saveFromResponse(Uri.parse(url), response.cookies);
// I had to transform the response for it to work
var body = await response.transform(Utf8Decoder(allowMalformed: true)).join();
return body;
}

Related

Why do my web api receive null values when posting to web API?

My Web API is receiving null value in Httpclient PostAsJsonAsync:
public static async Task<DefaultApiResponse<T>> PostList<T>(string url, string token, List<AddEventViewModel.Agenda> request)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
var content = JsonConvert.SerializeObject(request);
var buffer = System.Text.Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var httpResponse = await client.PostAsJsonAsync(url, byteContent);
var defaultresponse = JsonConvert.DeserializeObject<DefaultApiResponse<T>>(await httpResponse.Content.ReadAsStringAsync());
return defaultresponse;
}
Why?
I would try posting as a StringContent object instead
var content = new StringContent(
JsonConvert.SerializeObject(request),
Encoding.UTF8,
"application/json");
defaultresponse = await client.PostAsync(url, content);

Async/Await blocking on Task.WhenAll is called in Sitecore MVC project

I have to call a restsharp ExecuteTaskAsync, I have used await while executing the API and await to complete all tasks since it runs in loop, as soon as it hits await System.Threading.Tasks.Task.WhenAll(tasksList), then no it's blocked, no response in it.
Calling Async code:
Task<IEnumerable<AsyncResponse>> responseList = AddPAsync(id, id1);
To Execute Restsharp's ExecuteTaskAsync:
public static async Task<AsyncResponse> ExecuteApiAsync(RestRequest request, string url, dynamic identifier)
{
var restClient = new RestClient(url);
var cancellationTokenSource = new CancellationTokenSource();
var restResponse = await restClient.ExecuteTaskAsync(request);
return new AsyncResponse{ RestResponse = restResponse, Identifier = identifier };
}
Preparing request and calling RestSharp's ExecuteTaskAsync:
private async Task<IEnumerable<AsyncResponse>> AddPAsync(List<Participant> participantInfo, string registrationId)
{
foreach (var p in pinfo)
{
try
{
var request = new RestRequest(Constants.API_VERSION + Uri, Method.POST);
request.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
request.AddParameter(Constants.APP_JSON, JsonConvert.SerializeObject(p), ParameterType.RequestBody);
var response = Util.ExecuteApiAsync(request, Constants.END_POINT_URL_NAME, p.Identifier);
tasksList.Add(response);
}
catch (Exception ex)
{
}
}
await System.Threading.Tasks.Task.WhenAll(tasksList);
}
When it hits await Task.WhenAll then no response.
I have already tried:
`ConfigureAwait(false) - it is not working.
It is ASP.Net MVC application in sitecore.
Adding AsyncContext from Nito.AsyncEx worked.

Flutter multipart request along name value pair to server

I have to send to multipart request to server along with name value pair to server i mean id along with it. Can you please help me how to send id along with multipart files.
Upload() async {
var stream = new https.ByteStream(DelegatingStream.typed(Files[0].openRead()));
var length = await Files[0].length();
var stream1 = new https.ByteStream(DelegatingStream.typed(Files[1].openRead()));
var length1 = await Files[1].length();
var stream2 = new https.ByteStream(DelegatingStream.typed(Files[2].openRead()));
var length2 = await Files[2].length();
var uri = Uri.parse(openurl);
var request = new https.MultipartRequest("POST", uri);
var multipartFile1 = new https.MultipartFile('XX', stream, length,
filename: basename(Files[0].path));
var multipartFile2 = new https.MultipartFile('YY', stream1, length1,
filename: basename(Files[0].path));
var multipartFile3 = new https.MultipartFile('ZZ', stream2, length2,
filename: basename(Files[0].path));
//contentType: new MediaType('image', 'png'));
request.files.add(multipartFile1);
request.files.add(multipartFile2);
request.files.add(multipartFile3);
request.fields.addAll(other)
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
Thanks in advance
Sathish
I just had a problem similar to this. I had to upload an image along a key-value pair. I realize that I could upload the key-value pair using request.fields:
var request = new http.MultipartRequest("POST", uri);
request.fields['description'] = 'description';
request.fields['File-Name'] = 'FILENAME.jpg';
request.fields['qr_size'] = '3';

Flutter image_picker post upload an image

I am using the Flutter Plugin Image_picker to choose images so that I want to upload image after selected the image
Future<File> _imageFile;
void _onImageButtonPressed(ImageSource source) async {
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
}
I find this code in flutter documentation but its not work
var uri = Uri.parse("http://pub.dartlang.org/packages/create");
var request = new http.MultipartRequest("POST", url);
request.fields['user'] = 'nweiz#google.com';
request.files.add(new http.MultipartFile.fromFile(
'package',
new File('build/package.tar.gz'),
contentType: new MediaType('application', 'x-tar'));
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
Use MultipartRequest class
Upload(File imageFile) async {
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
var uri = Uri.parse(uploadURL);
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));
//contentType: new MediaType('image', 'png'));
request.files.add(multipartFile);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
Check this answer
This code works properly.
Used MultipartRequest class
void uploadImage() async {
File _image;
File pickedImage = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = pickedImage;
});
// open a byteStream
var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
// get file length
var length = await _image.length();
// string to uri
var uri = Uri.parse("enter here upload URL");
// create multipart request
var request = new http.MultipartRequest("POST", uri);
// if you need more parameters to parse, add those like this. i added "user_id". here this "user_id" is a key of the API request
request.fields["user_id"] = "text";
// multipart that takes file.. here this "image_file" is a key of the API request
var multipartFile = new http.MultipartFile('image_file', stream, length, filename: basename(_image.path));
// add file to multipart
request.files.add(multipartFile);
// send request to upload image
await request.send().then((response) async {
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}).catchError((e) {
print(e);
});
}
name spaces:
import 'package:path/path.dart';
import 'package:async/async.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
If you want the uploading function to return the server response, you can use toBytes() instead of transform(), in order to wait until data transmission is complete.
Future<String> upload() async {
String responseString = '';
// Pick image
final image = await ImagePicker().getImage(
source: ImageSource.gallery // or ImageSource.camera
imageQuality: 100,
maxWidth: 1000,
);
// Convert to File
final file = File(image.path);
// Set URI
final uri = Uri.parse('URL');
// Set the name of file parameter
final parameter = 'Name';
// Upload
final request = http.MultipartRequest('POST', uri)
..files.add(await http.MultipartFile.fromPath(parameter, file.path));
final response = await request.send();
if (response.statusCode == 200) {
responseString = String.fromCharCodes(await response.stream.toBytes());
}
return responseString;
}

Bad state: Stream has already been listened to Flutter error

I am calling an api. I am getting a streamed response after sending the request. But i cannot parse the response and convert it to String/JSON. This is where I am calling the api.
static Future<String> callDeviceListFetchApi() async {
Completer completer = new Completer();
String jsonResponse;
String url = Constants.BASE_URL + Constants.DEVICE_REGISTER_URL;
var client = new http.Client();
var request = new http.Request('GET', Uri.parse(url));
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
request.headers[HttpHeaders.AUTHORIZATION] = '<auth code>';
await client.send(request).then((response) {
response.stream.bytesToString().then((value) {
print(value.toString());
jsonResponse = value.toString();
completer.complete(jsonResponse);
});
}).catchError((error) {
print(error.toString());
});
return completer.future;
}
I am getting the error,
Bad state: Stream has already been listened to Flutter error. Any idea why this is happening?
There's a couple of things wrong with your code. I think you have a slight misunderstanding about how Async and Futures work in dart - you should re-read the docs and this tutorial (part 1 and part 2).
Basically, the problem is that you were returning a 'Future' from an async function. If you return a future from an async function, it has issues (I don't know why the analyzer doesn't catch that).
Future<String> callDeviceListFetchApi() async {
Completer completer = new Completer();
String url = "<url>";
var client = new http.Client();
var request = new http.Request('GET', Uri.parse(url));
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
request.headers[HttpHeaders.AUTHORIZATION] =
'<auth string>';
var response = await client.send(request);
String jsonResponse;
try {
var value = await response.stream.bytesToString();
print(value.toString());
jsonResponse = value.toString();
} catch (error) {
print(error.toString());
}
return completer.complete(jsonResponse);
}
Or not async:
Future<String> callDeviceListFetchApiNotAsync() {
String url = "<url>";
var client = new http.Client();
var request = new http.Request('GET', Uri.parse(url));
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
request.headers[HttpHeaders.AUTHORIZATION] =
'<auth string>';
Completer completer = new Completer();
return client.send(request).then((response) {
return response.stream.bytesToString();
}).then((value) {
print(value.toString());
return value.toString();
}).catchError((error) {
print(error.toString());
// if you use catchError, whatever you return from it
// is the value you'll get wherever you resolve the future.
return null;
});
}
But unless you're trying to do something I'm not seeing, there's a way easier way to do this (assuming all you want to do is get a string from a server):
Future<String> getList() async {
var response = await http.get("<url>", headers: {
HttpHeaders.CONTENT_TYPE: 'application/json',
HttpHeaders.AUTHORIZATION: '<auth string>',
});
if (response.statusCode == 200) {
return response.body;
} else {
throw Error();
}
}

Resources