PDA

View Full Version : Pentaho REST API Call from Asp.Net Web App throws error - 400 Bad request



sonu205
11-20-2013, 04:00 AM
Hi,

I am trying to create the Job from an Asp.Net Web Application hosted in IIS using two ways jQuery Ajax & C# Code but none of the approaches are working.

In case of Ajax request, I have set crossDomain:true, but still things are not working.
First of all i tried 3 different ways to authenticate the request as Basic auth, but i always get 401 unauthorized. Tried with dataType as joson & jsonp. Nothing works. Below is the code. I tried all other hit & trials, but none of them work from external application.

var jobDef =
{
"jobName": "my new job",
"inputFile": "/public/Steel Wheels/Country Performance (heat grid).xanalyzer",
"outputFile": null,
"simpleJobTrigger": {
"repeatInterval": 86400,
"uiPassParam": "DAILY",
"repeatCount": -1,
"startTime": "2013-11-25T04:30:00.000+02:00",
"endTime": "2013-11-28T23:59:59.000+02:00"
},
"jobParameters" : [
{
"name":"yearParameter",
"type":"string[]",
"stringValue": ["2003","2005"]
},
{
"name":"reportFormatType",
"type":"string",
"stringValue": "CSV"
}
]
} ;
$.ajax({
url: "http://admin:password@ServerName:8080/pentaho/api/scheduler/job",
crossDomain: true,

headers: {
"Accept": "text/json",
"Content-Type": "application/json"
},
// username:"admin",
// password:"password",
beforeSend: function (xhr) {
// xhr.withCredentials = true;
// xhr.setRequestHeader('Authorization', make_base_auth("admin", "password"));
},
async: false,
type: 'POST',
dataType: 'json',
data: jobDef,
success: function (res) {
alert(res);
},
error: function (e) {
debugger;
}

}).done(function (data) {

$("#create-job-results").html("Job created successfully. New job id is <code>" + data + "</code>")
})
.fail(function (e) {

$("#create-job-results").html("Error creating job"+e.statusText)
})


In case of C# code, REST API call to /api/schedule/jobs works fine to get all the scheduled job, but not the create job api works

// it works
void GetAllJobs()
{
var baseAddress = "http://ServerName:8080/pentaho/api/scheduler/jobs";

var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
// http.Credentials = new System.Net.NetworkCredential("admin", "password");

string authInfo = "admin" + ":" + "password";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
http.Headers.Add("Authorization", "Basic " + authInfo);

http.Accept = "application/json";
http.ContentType = "application/json; charset=utf-8";
http.Method = "GET";

var response = http.GetResponse();

var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd(); // it returns all the jobs
}

Issue: Above method works fine and returns all the scheduled jobs always, there is not way to pass parameter to filter the returned jobs, so to reduce the size of data returned from this API. is there is any way?

The method below doesn't work where I am trying to create a new job

void CreateJob()
{
var baseAddress = "http://ServerName:8080/pentaho/api/scheduler/job";

var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
// http.Credentials = new System.Net.NetworkCredential("admin", "password");

string authInfo = "admin" + ":" + "password";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
http.Headers.Add("Authorization", "Basic " + authInfo);

http.Accept = "text/plain";
http.ContentType = "application/json; charset=UTF-8";
http.Method = "POST";
((HttpWebRequest)http).ServicePoint.Expect100Continue = false;

string parsedContent = @"{
'jobName': 'my new job',
'inputFile': '/public/Steel Wheels/Country Performance (heat grid).xanalyzer',
'outputFile': null,
'simpleJobTrigger': {
'repeatInterval': 86400,
'uiPassParam': 'DAILY',
'repeatCount': -1,
'startTime': '2013-11-25T04:30:00.000+02:00',
'endTime': '2013-11-28T23:59:59.000+02:00'
},
'jobParameters' : [
{
'name':'yearParameter',
'type':'string[]',
'stringValue': ['2003','2005']
},
{
'name':'reportFormatType',
'type':'string',
'stringValue': 'CSV'
}
]
}";

ASCIIEncoding encoding = new ASCIIEncoding();
var encoder = new UTF8Encoding();
Byte[] bytes = UTF8Encoding.UTF8.GetBytes(parsedContent);

http.ContentLength = bytes.Length;

Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);

newStream.Close();
var response = http.GetResponse();

var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
}

in fiddler we see the error from pentato

HTTP Status 400 - Bad Request type Status report

message Bad Request

description The request sent by the client was syntactically incorrect.


and in Asp.Net, we get -
Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Please let me what are other ways to call the Pentaho 5.0 REST API. Do we need to change something in Pentaho/Tomcat server configuration?

Thanks!

sonu205
02-04-2014, 07:21 AM
I was able to resolve above issue, in case If I send the request from Asp.Net, but replacing single quote with double quotes. JSON like double quotes i guess.

string parsedContent = @"{
""jobName"": ""my new job"",
""inputFile"": ""/public/Steel Wheels/Country Performance (heat grid).xanalyzer"",
""outputFile"": null,
""simpleJobTrigger"": {
""repeatInterval"": 86400,
""uiPassParam"": ""DAILY"",
""repeatCount"": -1,
""startTime"": ""2013-11-25T04:30:00.000+02:00"",
""endTime"": ""2013-11-28T23:59:59.000+02:00""
},
""jobParameters"" : [
{
""name"":""yearParameter"",
""type"":""string[]"",
""stringValue"": [""2003"",""2005""]
},
{
""name"":""reportFormatType"",
""type"":""string"",
""stringValue"": ""CSV""
}
]
}";