With .NET web api, we need to post an array of byte arrays. So...
public async Task<IHttpActionResult> Post([FromBody] List<byte[]> documents)
documents is pulled from the body.
Is it possible to POST FromBody an array of byte arrays?
I know in JSON we can have...
[
"base64 encoded document 1 contents",
"base64 encoded document 2 contents"
]
Would the only option to base64 encode each document? This would give a signature of...
public async Task<IHttpActionResult> Post([FromBody] List<byte[]> documents)
#lcryder answered the question in the comments:
When you Base64 encode a byte array, the result is a String. The
controller would accept a String input for one document, or an array
of String for multiple documents. The controller would Base64 decode
the string into a byte array.
and
Base64 is the only way to go with byte[]'s moving across HTTP, there
will be controller characters in the bytes that will corrupt the
message.
Related
I'm trying to pass a json array* and a multipart file as form data. Image below.
postman screen
As you can see i changed the content type for the "metadata" key and i passed a little array just of one element for the only scope to try the call. But it doesn't work.
The Headers tab is in the image below:
header
(*) each element of the json array is formed by 3 elements: below an example
{"key" : "metadataKey", "value":"metadataValue", "redefinedKey":"aNewKey"}
How i can solve this problem?
Thanks to all!
Previously, i was able to store a base64 image using GridFsTemplate as below.
val imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary("base64 image string")
gridFsTemplate.store(ByteArrayInputStream(imageBytes), "imagename")
However the store() function of ReactiveGridFsTemplate takes in a parameter which is of type Flux<DataBuffer>. How can i convert a base64 image to that type?
I believe you can use AsyncStreamHelper.toAsyncInputStream with the base64 as a byte array.
I'm on my phone right now so I can't write an example, but you can check the second last method of the class here: https://github.com/BayviewComputerClub/smoothie-web/blob/master/src/main/java/club/bayview/smoothieweb/repositories/TestDataRepository.java
Given this ApiController:
public string TestString() {
return "The value is: " + 1.23;
}
public double TestDouble() {
return 1.23;
}
With the browser's language set to "fr-FR", the following happens:
/apiController/TestString yields
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">The value is: 1,23</string>
/apiController/TestDouble yields
<double xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1.23</double>
I would expect TestDouble() to yield 1,23 in the XML. Can anyone explain why this isn't the case and, more importantly, how to make it so that it does?
It is because the conversion from double to string happens at different stage for each API. For the TestString API, double.ToString() is used to convert the number to a string using CurrentCulture of the current thread and it happens when the TestString method is called. Meanwhile, the double number which is returned by TestDouble is serialized to string during the serialization step which uses GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Culture.
In my opinion, both should use InvariantCulture. On the consumer side, the values will be parsed and be formatted with the correct culture.
Update: this is only used for JsonFormatter. XmlFormatter doesn't have such a setting.
Update 2:
It seems (decimal) numbers need special converter to make it culture-aware:
Handling decimal values in Newtonsoft.Json
Btw, if you want o change data format per action/request, you can try the last piece of code of the following link: http://tostring.it/2012/07/18/customize-json-result-in-web-api/
The following is a sinatra route:
post '/accounts/:id/video' do |acc_id|
acc_id = params[:id].to_s
user = db_find('thrill.users',{_id: acc_id})
if user
db_update('thrill.users', { _id: acc_id},
{ '$set' =>
{ video: request.body.to_s }})
res(200, :sys_message => 'Video stored.')
else
res(201, :sys_message => "User not found")
end
end
It gets a response from the client with a video file in the form of a byte array. If I store it directly in the database, it is stored in the following format:
PhusionPassenger::Utils::TeeInput:0x007f48f8485b50
This has to be stored in the database in a format that can be retrieved later and sent back to the client. How can I read the byte array? The preferred format for storing would be in a form of a url which the client can access and stream the video. How can this be done?
From the Rack specification, about the request body (which is what request.body is:
The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.
The PhusionPassenger::Utils::TeeInput class meets these requirements. Calling to_s on an instance of this calls will just give the Ruby string representation of the object. To get the contents you need to call read, e.g.
{ video: request.body.read }
This will read the entire body into a string which you can then pass to Mongo.
I've got a set of data that exists in memory in a CSV format. I have this method in my controller:
public FileContentResult ItemsAsExcelExport(){
var model = _itemService.GetExcelExportModel();
return new FileContentResult(model.CSVData, model.MimeType){FileDownloadName = model.FileName};
}
The problem here is that my model.CSVData property returns a simple comma delimited set of values. I'm not sure how I can satisfy the fileContents argument of the FileContentResult contructor. It's asking for a byte array.
Thanks in advance.
Take a look at this question How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
The solution is
byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);