Getting 400 Bad Request error Spring mvc + angular JS ajax request - ajax

This is my ajax request
$scope.check = function(ids) {
$http.post("logreport/move", {"ids":ids})
.success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
};
& this is my controller
#RequestMapping(value = "/move", method = RequestMethod.POST)
public #ResponseBody
GenericResponse move(#RequestParam(value = "ids") String ids
) {
try {
System.out.println(ids);
} catch (Exception ex) {
System.out.println("INSIDE FAILED");
ex.printStackTrace(System.out);
return new GenericResponse("Failed", ex.getMessage(), 500);
}
return new GenericResponse("Success", "", 200);
}
Js logs
Remote Address:::1:8081
Request URL:http://localhost:8081/test/logreport/move
Request Method:POST
Status Code:400 Bad Request
Request Headersview
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:15
Content-Type:application/json;charset=UTF-8
Host:localhost:8081
Origin:http://localhost:8081
Referer:http://localhost:8081/test/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview
{ids:[ 1]}
ids: [ 1]
Response Headersview
Connection:close
Content-Length:971
Content-Type:text/html;charset=utf-8
Date:Mon, 12 May 2014 11:19:28 GMT
Server:Apache-Coyote/1.1

Related

NodeJS Post multipart data (Image Upload)

Hello I want to upload photo from my computer to https://generated.photos/anonymizer , I keep getting alert error in bodyz "Empty message body supplied with multipart form-data", Please help me with coding.
I want it to work like that https://i.imgur.com/CE6i4pE.png
var request = require("request");
var upfile = 'src/All/1.jpeg';
fs.readFile(upfile, function(err, content){
if(err){
console.error(err);
}
let url = "https://api.generated.photos/api/frontend/v1/images/similars";
let data = "";
data += "------WebKitFormBoundaryVIBbTfQEhARYeJqm" + "\r\n";
data += "Content-Disposition: form-data; name=\"file\"; filename=\"zeta\"\r\n";
data += "Content-Type: image/jpeg\r\n\r\n";
let payload = Buffer.concat([
Buffer.from(data, "utf8"),
new Buffer(content, 'binary'),
Buffer.from('\r\n------WebKitFormBoundaryVIBbTfQEhARYeJqm\r\nContent-Disposition: form-data; name=\"per_page\"\r\n\r\n1\r\n------WebKitFormBoundaryVIBbTfQEhARYeJqm--', "utf8"),
]);
let options = {
method: 'post',
url: url,
headers: {"Authorization": "API-Key Cph30qkLrdJDkjW-THCeyA", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36","Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryVIBbTfQEhARYeJqm" , "Content-Length": content.length },
body: payload,
};
request(options, function(error, response, bodyz) {
alert(bodyz);
// after i make it work i will do
// var response_data_parsed = JSON.parse(bodyz);
// alert(response_data_parsed.images.thumb_url)
});
});
Remove
"Content-Length": content.length
And all working. Thanks anyway

Headers Cache Control: No cache

I am doing post request to an api !
string url = "http://xxx.xxx.xx.xx/api/QMn/Create";
var client = new HttpClient();
client.BaseAddress = new Uri(url);
// client.Headers.ContentType = new MediaTypeHeaderValue("application/json");
StringContent content = new StringContent(JsonConvert.SerializeObject(DataToSave), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
var result = await response.Content.ReadAsStringAsync();
the Api recieving request is this :
public void Create([FromBody] IEnumerable<QMSRejection> DataToSave)
{
try
{
_QMSRejection.Create(DataToSave);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I am receiving this error I am not able to understand why ?
{StatusCode: 204, ReasonPhrase: 'No Content',
Version: 1.1, Content: System.Net.Http.StreamContent,
Headers: { Cache-Control: no-cache
Date: Fri, 28 Aug 2020 06:18:43 GMT Pragma: no-cache
Server: Microsoft-IIS/10.0 X-Android-Received-Millis: 1598595526870
X-Android-Response-Source: NETWORK 204 X-Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1598595526011
X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Expires: -1 }}
Do I have to pass headers but I only require to pass body !
I solved the issue the error was from API Side and not client side ! There were validations in the API that was causing it to give such errors !

jqGrid dataUrl not returning value from server

I am using dataUrl to populate my dropdown in form edit of jqGrid, but i am not getting any response from server. what am i missing? fiddler shows no error
editoptions: {
dataUrl: "CurrencySetting.aspx/GetDet",
buildSelect: function (response) {
alert(response.responseText);
var data = typeof response === "string" ?
$.parseJSON(response.responseText) : response;
s = "<select>";
s += '<option value="0">------</option>';
$.each(data, function () {
s += '<option value="1">' + this.CurrencyCd +
'</option>';
});
return s + "</select>";
}
}
server code:
public static string GetDet()
{
SysDataContext db = new SysDataContext();
var query = (from SC in db.SysCurrencySettings
select new
{
SC.CurrencyCd
}).ToList();
return JsonConvert.SerializeObject(query);
}
HTTP response
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 22 Oct 2015 11:10:23 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 15497
Connection: Close

Angularjs sending json data is getting 'POST 400 Bad request'

I'm trying to send json data from a form to a controller in spring through angular but I'm getting Bad request. Any idea what this could be.
Angularjs controller send function
$scope.sendaction = function() {
var dataPost = {
idTaskAction : $scope.data.action.idTaskAction,
idTask : $scope.data.action.idTask
};
idTaskAction = 2;
$http({
url : '../../taskaction/send/',
method : 'POST',
data : JSON.stringify(dataPost),
headers : {
'Content-Type' : 'application/json'
}
}).success(function(data, status) {
if (data.msg != '') {
console.log('Success sending action data.msg: ' + data.msg);
} else {
console.log('Success sending action data.errr' + data.error);
}
}).error(function(data, status, headers) {
console.log('#Error sending action status: ' + status);
console.log('#headers: ' + headers);
console.log('#failure message: ' + data.msg );
});
}
**Spring Controller ** that takes json data
#RequestMapping(value = "/taskaction/send",
method = RequestMethod.POST,
produces = "application/json")
#ResponseBody
public Map<String, Object> sendAction(Principal principal,
#RequestBody Map<String, Object> data, #PathVariable Long id) {
logger.info("Task controller /taskaction/send ...");
String actionname = (String) data.get("actionname");
String description = (String) data.get("description");
String duration = (String) data.get("duration");
System.out.println("*****>>>>>>" + actionname + "|" + description + "|"
+ duration);
Map<String, Object> rdata = new HashMap<String, Object>();
TaskAction action = null;
rdata.put("success", true);
return rdata;
}
And what I'm receiving from chrome inspector request is
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/myTasks/taskaction/send/
Request Method:POST
Status Code:400 PeticiĆ³n incorrecta
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:18
Content-Type:application/json
Cookie:JSESSIONID=70CB35CA8E24E6ED7E7EB44FADB31BDD
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/myTasks/task/upd/8
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Request Payloadview source
{idTaskAction:2}
Response Headersview source
Connection:close
Content-Language:es
Content-Length:990
Content-Type:text/html;charset=utf-8
Date:Sat, 20 Sep 2014 09:00:14 GMT
Server:Apache-Coyote/1.1
The request just got one parameter {idTaskAction:2} and in angular controller dataPost variable I'm saving 2, idTask and idTaskAction.

IE lags on sending POST data

Recently I found very strange behavior of IE8/9 when sending XHR data. I'd like to ask you if you have ever seen such behavior? Is it IE bug? How can I protect my server from "empty" request generated by IE? Here are more details:
I found out that when your page tries to send XHR data and JavaScript freezes or lags browser, IE lags sending POST data to server. To make it clear, here are steps to reproduce:
Create simple server which dumps POSTs received. I've used Node.JS for tests. You can find corresponding codes at the bottom of the post.
Open localhost:8080 in IE, open debug window and start debuging scripts with breakpoint set in line 51 of file localhost:8080
Hit Send by JQuery. You should have your javascript waiting on the breakpoint and server logged message:
--------------------------------------
14:04:19
REQUEST: /
HTTP HEADER:
accept = text/html, application/xhtml+xml, */*
accept-language = pl-PL
user-agent = Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
accept-encoding = gzip, deflate
host = localhost:8080
connection = Keep-Alive
--------------------------------------
14:04:35
REQUEST: /test
HTTP HEADER:
accept = */*
content-type = application/json
x-requested-with = XMLHttpRequest
referer = http://localhost:8080/
accept-language = pl
accept-encoding = gzip, deflate
user-agent = Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
host = localhost:8080
content-length = 24
connection = Keep-Alive
cache-control = no-cache
When you release JavaScript from the breakpoint server reports incoming data by new line:
POST: {"data1":10,"data2":123}
I've done the same test on Opera 12.15 and Chrome 27.0.1453.94 m and, as expected, POST data are sent immediately.
-- source codes --
Code for node.js server:
var http = require('http');
var qs = require('querystring');
var fs = require('fs');
http.createServer(function (req, res) {
console.log('--------------------------------------');
console.log(new Date().toLocaleTimeString());
console.log('REQUEST: ' + req.url);
console.log('HTTP HEADER: ');
for (var header in req.headers) {
console.log(' ' + header + ' = ' + req.headers[header]);
}
switch (req.method) {
case 'POST':
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function() {
console.log('POST: ' + body);
});
res.writeHead(200, {'Content-type': 'application/json'});
res.end(JSON.stringify({ succeeded: true, value: 'Hello world!' }));
break;
case 'GET':
fs.readFile('./servertest.html', function (err, data) {
if (err) {
throw err;
}
res.writeHeader(200, {'Content-type': 'text/html'});
res.end(data);
});
break;
}
}).listen(8080, 'localhost');
console.log('listening on localhost:8080');
Code for servertest.html:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<button onclick="send()">Send by JQuery</button>
<button onclick="sendXHR()">Send by XHR</button>
</body>
<script type="text/javascript">
var _xhr;
function sendXHR() {
_xhr = new XMLHttpRequest();
new XHRStateChangeListener();
var url = '/testXHR';
var method = 'POST';
_xhr.open(method, url);
_xhr.setRequestHeader("Content-Type", "application/json");
_xhr.send(JSON.stringify({test: 'this is a test', aoawiejf:23423}));
}
function XHRStateChangeListener() {
callback = function() {
var msg;
if (_xhr.readyState == 4 && /200|304/.test(_xhr.status)) {
alert('ready: ' + _xhr.responseText);
}
};
_xhr.onreadystatechange = callback;
}
function send() {
$.ajax({
url: '/test',
type: 'POST',
data: JSON.stringify({data1: 10, data2: 123}),
contentType: 'application/json',
error: function(xhr, status, err) {
alert('Error: ' + err + '\nStatus: ' + status);
},
success: function (data) {
alert('succeeded! data: ' + data);
}
});
}
</script>
</html>
You are using sync-mode, right is async-mode:
replace
_xhr.open(method, url);
by
_xhr.open(method, url, true);//Async mode
Note: Remember "onreadystatechange" is only for async-mode
the problem may also be in this function (just a guess):
JSON.stringify
try sending empty data, ie: data:""

Resources