i want to post Form Data like that.
what should i prepare for sending image file data?
i have Uri, type, filename, size.
then will use fetch for it.
Content-type in header is 'multipart/formdata'
thanks for helping
You should have an upload function, which should look like this:
upload(url, data) {
let options = {
headers: {
'Content-Type': 'multipart/form-data'
},
method: 'POST'
};
options.body = new FormData();
for (let key in data) {
options.body.append(key, data[key]);
}
return fetch(requestUrl, options)
.then(response => {
return response.json()
.then(responseJson => {
//You put some checks here
return responseJson;
});
});
}
And you call it this way, sending the image blob path:
this.upload('http://exampleurl.com/someApiCall', {
file: {
uri: image.path,
type: image.mime,
name: image.name,
}
}).then(r => {
//do something with `r`
});
You need to create an instance of FormData and pass that as the body to fetch, like so:
const data = new FormData()
data.append("something", something)
fetch(url, { method: 'POST', body: form })
React Native code
fetch("url" ,{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
method:'POST',
body: JSON.stringify(this.state.imageholder)
}).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
throw error;
});
Spring boot code
#PostMapping(value="/",consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> saveCustomerOrder(#RequestBody String[] file) throws SerialException, SQLException {
TestImg imgObj=null;
for (String img : file) {
imgObj=new TestImg();
byte[] decodedByte = Base64.getDecoder().decode(img);
Blob b = new SerialBlob(decodedByte);
imgObj.setImg(b);
testRepo.save(imgObj);
}
Related
I'm using the recognizeText REST endpoint from javascript running locally on my dev machine. I can successfully call the endpoint, get the operation-location url for the result and send a GET request to that url.
The issue is the return from the operation-location url is 200 success (meaning the operation has completed and doesn't need more time), but the body of the result is always empty.
How can I get the extracted text from the response?
My code:
var subscriptionKey: string = "my key";
var endpoint: string = "https://eastus.api.cognitive.microsoft.com/";
var uriBase: string = endpoint + "/vision/v2.0/recognizeText?mode=Printed";
const fetchData = {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": subscriptionKey
},
body:
'{"url": "https://www.bing.com/th/id/OIP.nZoyhANat4WNndv0jeoXFAHaLp?w=183&h=289&c=7&o=5&dpr=1.5&pid=1.7"}',
method: "POST"
};
fetch(uriBase, fetchData).then(data => {
var operationLocation = data.headers.get("Operation-Location");
if (operationLocation) {
const resultFetchData = {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": subscriptionKey
},
method: "GET"
};
setTimeout(function(operationLocation, resultFetchData) {
fetch(operationLocation, resultFetchData).then(resultData => {
alert(JSON.stringify(resultData, null, 2));
});
}, 10000);
}
});
}
There is something wrong with your fetch request code block, try this :
fetch(uriBase, fetchData).then(data => {
var operationLocation = data.headers.get("Operation-Location");
if (operationLocation) {
const resultFetchData = {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": subscriptionKey
},
method: "GET"
};
setTimeout(()=> {
fetch(operationLocation, resultFetchData).then(resultData => {
return resultData.json();
}).then((data)=>{
console.log(JSON.stringify(data, null, 2));
});
},10000);}
});
Result :
I'm trying to convert an ionic app in an ionic 2 app and im strugle trying to remake the login progress.
I wrote this in my ionic 2 app:
loginUser(){
localStorage.setItem('username', this.username);
localStorage.setItem('password', this.password);
localStorage.setItem('company', this.company);
this.loginData = {};
this.loginData.UserID = this.username;
this.loginData.CompanyID = this.company;
this.loginData.Password = this.password;
let body = {"jsonLogin": JSON.stringify(this.loginData)}
let link = 'working link';
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
});
let options = new RequestOptions({headers: headers});
this.http
.post(link, body, options)
.map(res => res.json())
.subscribe(
data => {
console.log(data);
//this.navCtrl.push(MenuPage);
}, err => {
console.log(err);
});
}
While in the ionic 1 app this was the code:
$scope.loginUser = function () {
json = {};
json.UserID = $scope.data.username;
json.CompanyID = $scope.data.company;
json.Password = $scope.data.password;
$http({
method: 'POST',
data: {
"jsonLogin": JSON.stringify(json)
},
url: 'working link',
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
}
My problem is in the POST, when I post with my code of the ionic 2 app this is what my Form Data looks like:
{
"jsonLogin": "{\"UserID\":\"admin\",\"CompanyID\":\"test\",\"Password\":\"pass\"}"
}:
While the ionic 1 app Form Data looks like this:
jsonLogin:{"UserID":"admin","CompanyID":"test","Password":"pass"}
The POST is working since I got an error message from server, I just don't know how I can format the data to do a correct POST.
Thank you in advance for the help.
EDIT:
Got it working by added a new header:
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*'
});
And send a hardcoded string:
createStringForLogin(username: any, company: any, password: any){
return 'jsonLogin={"UserID":"'+username+'","CompanyID":"'+company+'","Password":"'+password+'"}';
}
I think
let body = {"jsonLogin": this.loginData};
should solve your problem. Let me know if it does not work.
Also, if you are having trouble in setting parameters to this.loginData. Set them like this:
this.loginData = {};
this.loginData['UserID'] = this.username;
this.loginData['CompanyID'] = this.company;
this.loginData['Password'] = this.password;
Here is my WebAPI POST method which expects BookingDetail and BookingVenue objects:
[HttpPost]
[ValidateUserSession]
public JsonResult CheckBooking(BookingDetail BookingDetail, BookingVenue objBV)
{
try
{
if (BookingDetail != null && objBV != null)
{
bool result = Ibook.checkBookingAvailability(BookingDetail, objBV);
if (result == false)
{
return Json("NotAvailable");
}
else
{
return Json("Available");
}
}
else
{
return Json("Available");
}
}
}
Angular code from where I'm getting the values from UI and making a post passing these 2 objects:
this.checkbookingavailability = function (Book) {
var BookingVenueObj = {
EventTypeID: Book.EventSelected,
VenueID: Book.Venueselected,
GuestCount: Book.NoofGuest,
};
var BookingDetailObj = {
BookingDate: Book.BookingDate
};
var response =
$http({
method: "POST",
url: "/Booking/CheckBooking/",
headers: {
'RequestVerificationToken': $cookies.get('EventChannel')
},
data: { BookingDetail: BookingDetailObj, BookingVenue: BookingVenueObj }
});
return response;
}
Problem is in my WebAPI code, both the objects as null
You can only pass one object in the body so I would recommend you to create a new DTO "BookingDto" for that containing BookingDetail and BookingVenue as member and change the signature of your WebAPI to this:
[HttpPost]
[ValidateUserSession]
public JsonResult CheckBooking([FromBody]BookingDto bookingObj)
You need to serialize JSON object which you are sending to server just by calling JSON.stringify over object.
var response =
$http({
method: "POST",
url: "/Booking/CheckBooking/",
headers: {
'RequestVerificationToken': $cookies.get('EventChannel')
},
data: JSON.stringify({
BookingDetail: BookingDetailObj,
BookingVenue: BookingVenueObj
})
});
return response;
As dear Pankaj mentioned you need to Serialize your data objects with Stringify function in javascript, Also consider that you must mention that this http request contain Application/JSON content. All these can be shown here:
var response =
$http({
method: "POST",
url: "/Booking/CheckBooking/",
headers: {
'RequestVerificationToken': $cookies.get('EventChannel'),
'Content-Type' : 'application/json'
},
data: JSON.stringify({
BookingDetail: BookingDetailObj,
BookingVenue: BookingVenueObj
})
});
return response;
I have a problem here when I am trying to push data with angularjs controller. But what ever I do (IFormFile file) is always empty. There are only some examples with razor syntax but no examples how to do it with angular or jquery.
HTML:
<form class="form-body" enctype="multipart/form-data" name="newFileForm" ng-submit="vm.addFile()"><input type="file" id="file1" name="file" multiple ng-files="getTheFiles($files)"/></form>
Directive:
(function() {
'use strict';
angular
.module('app')
.directive('ngFiles', ['$parse', function ($parse) {
function fn_link(scope, element, attrs) {
var onChange = $parse(attrs.ngFiles);
element.on('change', function (event) {
onChange(scope, { $files: event.target.files });
});
};
return {
link: fn_link
};
}]);
})();
Controller
var formdata = new FormData();
$scope.getTheFiles = function ($files) {
angular.forEach($files, function (key, value) {
formdata.append(key, value);
});
};
vm.addFile = function () {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "undefined");
xhr.send(formdata);
}
Asp.net core webapi:
[HttpPost]
public async Task<IActionResult> PostProductProjectFile(IFormFile file)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
....
return ...;
}
I have also tried to do it with formdata, as it is constructed when you post it with razor syntax. Something like this:
dataService.addFile(formdata, {
contentDisposition: "form-data; name=\"files\"; filename=\"C:\\Users\\UserName\\Desktop\\snip_20160420091420.png\"",
contentType: "multipart/form-data",
headers: {
"Content-Disposition": "form-data; name=\"files\"; filename=\"C:\\Users\\UserName\\Desktop\\snip_20160420091420.png\"",
'Content-Type': "image/png"
},
fileName: "C:\\Users\\UserName\\Desktop\\snip_20160420091420.png",
name: "files",
length : 3563
}
Also instead of formData to provide raw file as I wrote in comment. But still nothing happens
IFormFile will only work if you input name is the same as your method parameter name. In your case the input name is 'files' and the method parameter name is 'file'. Make them the same and it should work.
This is how to do it with angularjs:
vm.addFile = function () {
var fileUpload = $("#file").get(0);
var files = fileUpload.files;
var data = new FormData();
for (var i = 0; i < files.length ; i++) {
data.append(files[i].name, files[i]);
}
$http.post("/api/Files/", data, {
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).success(function (data, status, headers, config) {
}).error(function (data, status, headers, config) {
});
}
And in web Api:
[HttpPost]
public async Task<IActionResult> PostFile()
{
//Read all files from angularjs FormData post request
var files = Request.Form.Files;
var strigValue = Request.Form.Keys;
.....
}
Or like this:
[HttpPost]
public async Task<IActionResult> PostFiles(IFormCollection collection)
{
var f = collection.Files;
foreach (var file in f)
{
//....
}
}
You can do it also with kendo upload much simpler:
$("#files").kendoUpload({
async: {
saveUrl: dataService.upload,
removeUrl: dataService.remove,
autoUpload: false
},
success: onSuccess,
files: files
});
From the answer of #Tony Steele.
Here is the code sample (Where to change/take care of)
.NET Core 3.1 LTS
[Route("UploadAttachment")]
[HttpPost]
public async Task<IActionResult> UploadAttachment(List<IFormFile> formFiles)
{
return Ok(await _services.UploadAttachment(formFiles));
}
AngularJS
var formFiles = new FormData();
if ($scope.files != undefined) {
for (var i = 0; i < $scope.files.length; i++) {
formFiles.append('formFiles', $scope.files[i]);
}
}
I found the request module in js cannot handle gzip or inflate format http response correctly.
for example:
request({url:'some url'}, function (error, response, body) {
//if the content-encoding is gzip, the body param here contains binaries other than readable string. And even worse after you convert the body to buffer, u even can not gunzip it.
}
so I want to use the sample code in official docs.
var request = http.get({ host: 'izs.me',
path: '/',
port: 80,
headers: { 'accept-encoding': 'gzip,deflate' } });
request.on('response', function(response) {
var output = fs.createWriteStream('izs.me_index.html');
switch (response.headers['content-encoding']) {
// or, just use zlib.createUnzip() to handle both cases
case 'gzip':
response.pipe(zlib.createGunzip()).pipe(output);
break;
case 'deflate':
response.pipe(zlib.createInflate()).pipe(output);
break;
default:
response.pipe(output);
break;
}
});
The problem is that the code is writing the webpage into a file, I hope it can write the page into a string, so that i can process the page. I could not find any class like 'StringStream'.
If anyone has any idea on this,it will be great.
Pipe the response to the gzip stream and use it as you would use the original response object.
var req = http.request(options, function(res) {
var body = "";
res.on('error', function(err) {
next(err);
});
var output;
if( res.headers['content-encoding'] == 'gzip' ) {
var gzip = zlib.createGunzip();
res.pipe(gzip);
output = gzip;
} else {
output = res;
}
output.on('data', function (data) {
data = data.toString('utf-8');
body += data;
});
output.on('end', function() {
return next(false, body);
});
});
req.on('error', function(err) {
next(err);
})
simplified example:
var https = require('https');
var gunzip = require('zlib').createGunzip();
var options = {
host: 'api.stackexchange.com',
path: '/2.1/info?site=stackoverflow'
};
https.get(options, function(res) {
var body = '';
res.pipe(gunzip);
gunzip.on('data', function (data) {
body += data;
});
gunzip.on('end', function() {
console.log(JSON.parse(body));
});
});
I ran into a similar issue and wanted to continue using the request library instead of the built-in http module. I've discussed two working approaches here: http://nickfishman.com/post/49533681471/nodejs-http-requests-with-gzip-deflate-compression. One of them is similar to #Teemu's answer, while the other uses streams.
request module handles the gzip responses. All we have to do is to set 'gzip' attribute in the opts. For detailed explaination please visit the below linke. There I have clearly explained with example.
https://stackoverflow.com/a/38582506/5878471
The answers of #Dawid and #Teemu sometimes brake the chars in the answer in case of utf-8 encoding. This code works much better:
function getGzipped(url, cb) {
// downloads gzipped file
http.get(url, function(res) {
let chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
let buffer = Buffer.concat(chunks);
zlib.gunzip(buffer, function(err, decoded) {
if (err) throw err;
cb(decoded && decoded.toString());
});
});
});
}
Gathered it all together:
gzip, deflate support
JSON body decoding
Correct utf8 finish decoding
Promise-like function (async/await support)
Code:
import * as https from "https";
import * as zlib from "zlib";
const send = (url, config) => {
return new Promise(async (resolve, reject) => {
let req;
try {
req = https.request(url, config, (res) => {
let body: any = [];
let output;
res.on("error", (e) => reject(e));
if (res.headers["content-encoding"] == "gzip") {
const gzip = zlib.createGunzip();
res.pipe(gzip);
output = gzip;
} else if (res.headers["content-encoding"] == "deflate") {
output = zlib.createInflate();
res.pipe(output);
} else {
output = res;
}
output.on("data", (data) => body.push(data));
output.on("end", () => {
try {
body = Buffer.concat(body).toString();
if (res.headers["content-type"].indexOf("application/json") > -1) body = JSON.parse(body);
} catch (e) {
reject(e);
return;
}
resolve({
data: body,
status: res.statusCode,
statusText: res.statusMessage,
headers: res.headers,
request: {
url,
config
}
});
});
});
req.on("error", (e) => reject(e));
req.end();
} catch (e) {
reject(e);
}
});
};
console.log(await send("https://www.google.com/", { headers: { "User-Agent": "Google" } }));