Dart - Converting a http response to a buffer - dart

how do i convert a http response to a buffer? im using the http pub package https://pub.dev/packages/http and one of the endpoints of an api im using is returning an image file, i want to convert it to a buffer. in JS i'd just do
const result = await res.buffer();
but how do i do it in dart?
I tried a few different methods of the Response class but couldnt get my head around it

Node describes Buffer as
The Buffer class is a subclass of JavaScript's Uint8Array class
So it may safe assume that you can use the Response.bodyBytes (which is a dart Uint8List type) field as jamesdlin suggested.

Related

Post-function custom lua code to manipulate JSON response body

I’m trying to write a custom plugin to transform response body. I could’ve used a response transformer plugin, but my response body json is complex, so I want to remove few fields from it.
I tried using post-function plugin to write my custom lua code but it doesn’t let me import cjson, so I’m unable to decode the response and remove specific keys from it.
My lua code in body_filter:
local cjson = require(“cjson”)
local body = cjson.decode(kong.response.get_raw_body())
-- set custom key’s value to 1
body.subKeyFoo.subSubKey = 1;
This is what I get:
require cjson not allowed within sandbox “kong”
The sandbox is enabled, this is to protect arbitrary Lua code from doing dangerous things. See the docs on how to disable the sandbox. Link: https://docs.konghq.com/gateway/latest/reference/configuration/#untrusted_lua check untrusted_lua_xxx options (3 in total)

Using pythonnet to pass stream to .net DLL

I'm working with a .NET DLL file processor, but can't seem to get the stream passing working. I have the following relevant methods.
EmbedFile(string): bool
EmbedFile(Stream): bool
When using the string version, it works as expected when given a filename.
encoder.EmbedFile("test.dat")
However, I'm not sure what to pass to the stream version. I've tried io.BytesIO and a file handle, but both give me the following.
TypeError: No method matches given arguments for EmbedFile
What is the correct oject to pass to a .NET method that takes a Stream parameter?
I know this is a late reply, but have you tried importing a .NET class that inherits from Stream, then constructing an instance in Python.
import clr
from System import File
from System.IO import FileStream
def main():
path = "path\\to\\file.dat"
clrFile = File(path)
clrFileStream = FileStream(clrFile)
returnValue = EmbedFile(clrFileStream)
Note: this could require additional import(s) if more .NET parameters are present in the derived Stream's constructor.

Custom Flutter Notification Plugin

I'm currently developing a notification plugin to be used with my music application that I'm converting over to Flutter. Thus far it's all been working perfectly, however though I ran into a problem which I'm not sure how to handle.
My plugin requires an image which is displayed in the notification, The images are all hosted and fetched via url (https://example.com/img.png) so that eliminates the loading via file system
Now the problem is that, I would like to keep the plugin as lightweight as possible (would rather not add Glide etc).
Is there anyway I can pass the bitmap directly from flutter to the plugin ? perhaps the same way that we can pass strings ?
static Future example(String data) async {
await _channel.invokeMethod('example', {
'data' : data
});
}
Thanks in advance guys, Any advice is appreciated.
The StandardMessageCodec which converts between Dart and native types handles
acyclic values of these forms:
null
[bool]s
[num]s
[String]s
[Uint8List]s, [Int32List]s, [Int64List]s, [Float64List]s
[List]s of supported values
[Map]s from supported values to supported values
so you can pass a Uint8List (in place of String data) in your example, and you will get a byte[] or FlutterStandardTypedData on the native side for Android and iOS respectively.
It's not clear whether you need the png on the native side or the decoded bitmap.
To fetch the png you could use the http package
import 'dart:typed_data';
import 'package:http/http.dart';
Uint8List png =
(await get('http://www.barcodes4.me/barcode/c39/123456.png')).bodyBytes;
and pass Uint8List png to invokeMethod. If you need to convert to a bitmap first (though you should avoid this if you can, as the bitmap will be much larger than the png), use the image package.
import 'package:image/image.dart';
Image image = decodeImage(png); // if known to be a PNG, could call decodePng
Map<String, dynamic> imageData = {
'width': image.width,
'height': image.height,
'bitmap': image.getBytes(),
};
and pass Map imageData to invokeMethod. This will appear at the native end as a java.util.HashMap or NSDictionary.

Opencv - create png image

As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in img tag to achieve streaming.
Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.
The thing is, I am having some problems with encoding my data to PNG using OpenCV library that is already used in the project.
Firstly, some code:
websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix->rows * websocketBrokerStructure.matrix->cols * websocketBrokerStructure.matrix->step;
websocketBrokerStructure.imageDataSent = 0;
but I am getting strange error during execution of the second line:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
and I am a bit confused why I am getting this error from my code.
Also I am wondering if I understand it right: after invoking cvEncodeImage (where bgrImage is IplImage* with 3 channels - BGR) I just need to iterate through data member of my CvMatto get all of the png encoded data?
The cvEncodeImage function takes as its first parameter the extension of the image you want to encode. You are passing 0, which is the same thing as NULL. That's why you are getting the message NULL not valid.
You should probably use this:
websocketBrokerStructure.matrix = cvEncodeImage(".png", websocketBrokerStructure.bgrImageToSend, 0);
You can check out the documentation of cvEncodeImage here.
You can check out some examples of cvEncodeImage, or its C++ brother imencode here: encode_decode_test.cpp. They also show some parameters you can pass to cvEncodeImage in case you want to adjust them.

downloading and storing files from given url to given path in lua

I'm new with lua but working on an application that works on specific files with given path. Now, I want to work on files that I download. Is there any lua libraries or line of codes that I can use for downloading and storing it on my computer ?
You can use the LuaSocket library and its http.request function to download using HTTP from an URL.
The function has two flavors:
Simple call: http.request('http://stackoverflow.com')
Advanced call: http.request { url = 'http://stackoverflow.com', ... }
The simple call returns 4 values - the entire content of the URL in a string, HTTP response code, headers and response line. You can then save the content to a file using the io library.
The advanced call allows you to set several parameters like HTTP method and headers. An important parameter is sink. It represents a LTN12-style sink. For storing to file, you can use sink.file:
local file = ltn12.sink.file(io.open('stackoverflow', 'w'))
http.request {
url = 'http://stackoverflow.com',
sink = file,
}

Resources