GetMethod.getResponseBodyAsString() to xml object - javascript

using Javascript HttpClient i am running a get method on WebService which works fine and then i store Response in a variable resp_va as shown below.
snip from code below.
var httpGetMethod = new GetMethod(url);
httpClient.executeMethod(httpGetMethod);
var statuscode = httpGetMethod.getStatusCode();
var resp_va = httpGetMethod.getResponseBodyAsString();
although output is in XML Format but to me Looks like it is string and therefore i am unable to parse it. Question is how can i convert "resp_va" in XML object before parsing it further?

Related

readFileSync is returning empty string

Reading a file using readFileSync is returning an empty string.
I am reading contents of a file using link which is retrieved from the database. It is working.
But I am unable to read the file using link which is stored in a variable.
My code is given below:
var result1={};
var filename="/home/rgukt/Project 2.0/CCMS/Admin/Challenges/"+(req.body.challenge_name)+"/"+(req.user.username)+"_expected_output"+(result[i].test_case_id)+".txt" ;
result1.output = fs.readFileSync(result[i].output,'utf8'); // working
result1.expected_output = fs.readFileSync(filename,'utf8'); // not working

Can't call a part of an object after parsed as JSON in Google Apps Script

I'm trying to make some code that can pull from an API and pull out a piece from the response. My code is below.
var response = UrlFetchApp.fetch('https://[API_URL]', options);
Logger.log(response.getContentText());
var firstCall = response.getContentText();
var JsonObj = JSON.parse(firstCall);
Logger.log(firstCall['id']);
sheet.appendRow(['successfully connected to API.']);
sheet.appendRow([Logger.getLog()]);
A sample response from the API is below.
[{"id":12345678901234567,"name":"email#email.com - someText"}]
When I try to run the first code, it completes the code, logging the above line and undefined. My goal is to get just the ID from the string. Thanks for your help!
You're almost there. To keep it simple, do it like this:
function getURLFromSite(){
var response = UrlFetchApp.fetch('https://jsonplaceholder.typicode.com/users');
var JsonObj = JSON.parse(response);
Logger.log(JsonObj[1].name);
}
Here's JsonObj is like an array which you can access using JsonObj[0], JsonObj[1] and JsonObj[2].
To access properties like id or name, just use the dot notation like JsonObj[0].id or JsonObj[1].name.

Accessing C# DataObject data part from javascript

Is it possible to access the C# DataObject data from javascript?
DataObject data = new DataObject();
data.SetText(stringBuilder.ToString());
data.SetData("application/json", itemsJson);
Clipboard.Clear();
Clipboard.SetDataObject(data);
var x = Clipboard.GetData("application/json");
In the C# side, x will be the json string. But it lost at js side.
EDIT
With
text = ev.clipboardData.getData("text/plain");
I've got the text, but:
var json = ev.clipboardData.getData("application/json");
nothing will be in json.
UPDATE
As I see, I can not access the data part of the dataobject from javascript. I think this is for some security reason, but not sure.

recieving broken JSON data in javascript

I have PageMethod in javascript which is receiving JSON data from C#.
In C# its getting full xml data from database and converting into JSON and sending back to PageMethod.
JSON Converted data is about 33kb, but i'm not able to receive full data in javascript. I'm receiving only 9 kb of data. any solution for getting full data in java script.
PageMethod.methodName(onSuccess,OnFail);
function OnSuccess(result)
{
alert(result);
}
function OnFail()
{
alert("Error");
}
C# code as follows,
ParamResult objParamResult = new ParamResult();
objParamResult.ResultDt = string.Empty;
DataTable XmlMainSub = objCBTag.getParamPickupDetailsDB();
string myData = XmlMainSub.Rows[0][0].ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(myData);
string jsonText = JsonConvert.SerializeXmlNode(doc);
return jsonText;
instead of
string jsonText = JsonConvert.SerializeXmlNode(doc);
you can use
string jsonText = new JavaScriptSerializer().Serialize(doc).toString();
you need to use namespace for this
using System.Web.Script.Serialization;
After i made lot of research, i found that its not possible to send JSON data from C# to javascript which is more than 8KB or 9KB in size.
And i solved this problem by making use of c# generics which is Dictionary which contains Key and Value Pair. I Tried to loop XML Data which is coming from database and stored in a dictionary object.
Then i passed it to javascript. There i able to receive full data without any error.

How to parse an XML response using Runscope

I have been using Runscope default library "marknote XML Parser" to parse XML documents. In all the endpoints so far it was working well. However in the new endpoint I am trying to parse it is parsing a null object. This is the code I am using:
var parser = new marknote.Parser();
var doc = parser.parse(response.body);
The thing is, when I try the information using the 'XML body' assertions of Runscope using X-path it works like a charm, but when I use the library it does not parse.
Any ideas?

Categories

Resources