In my application, there is an endpoint that sends me the raw contents of a Yaml file in response to an AJAX call. I want to display them as they are in UI. The console throws an obvious error, which is for invalid JSON. How would I do it?
Update:
This is the snippet used for reading the file and sending the response.
filename = __file__ # Select your file here.
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='text/plain')
response['Content-Length'] = os.path.getsize(filename)
return response
Is there a way I could form a dictionary there with the contents of the file and then send the response?
From server, use jsonify on the raw content, pack it and ship it over to client.
repacked_json = json.dumps(raw_yaml_data)
json_obj = json.loads(repacked_json)
return jsonify(result = json_obj)
Related
I have an endpoint I fetch that returns a CSV. I'm trying to export that string as a CSV file on click. The header in the response contains content-dispostion and its content type is 'text/csv'.
How can I do this?
let download = await function(){
let response = await fetch('www.endpointblahblah.com')
//The rest of the function needs to take the string that's returned from the response and export as a CSV file by downloading it.
}
Turning the response into JSON throws an error. It should absolutely do this because a JSON object is not returned, it is text.
Thanks in advance.
I want to parsing this response from http get and display the value into a detail modal but I dont know how to parse it. I have using arc to show me the response of my http get,here is my image of my response using arc.
here is my.js file
$http.get('.....a='+$scope.month).then(function(response){
$scope.coba = response.data;
Use JSON.stringify(response.data) or angular.toJson(response.data), it will parse the http response into JSON.
If it is failing, means your response is not exact JSON
I'm writing a test using SlimerJS for a website and need to check the response body coming from the server. I'm using the following piece of code to get the response:
page.onResourceReceived = function (response) {
console.log(JSON.stringify(response));
};
I do receive the response but since by default to prevent too much memory usage SlimerJS keeps the response body empty I too receive an empty body, unless I tell it not to keep the body empty for certain formats using something like this:
webpage.captureContent = [ /css/, /image\/.*/ ]
I understand this works well for files with extensions like css,jpg and avi, but what about an AJAX response coming from the server. The response is in JSON format and the response body is left empty.
By looking at the response header you can tell that the response type is in text/html so by using the following code you can get the body.
page.captureContent = [/text/, /html/]
I use the below code to send data (GET) from python to javascript.
in javascript:
$.get('http://localhost:5000/api/scan').success(function(res) {
obj = JSON.parse(res);
if(obj['channel'] == "1"){
document.getElementById("channelZero").innerHTML = "1";
}});
in python:
channels_str = channels.getvalue()
data['channel'] = channels_str
return dumps(data)
how can I send data from javascript to python?
In case
$.get('http://localhost:5000/api/scan').success(function(res) {
//your code
}});
you request scan, but if you add "?who=Masha" you send a parameter who with a value "Masha" as get request parameter.
$.get('http://localhost:5000/api/scan?who=Masha').success(function(res) {
//your code
}});
Then if your python get response (typically def do_GET(self):) has:
data['channel'] = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('who', None)
Note that you need to import urlparse (In Python3, you'd use urllib.parse)
import urlparse
From w3schools
The GET Method
Note that the query string (name/value pairs) is sent
in the URL of a GET request:
/test/demo_form.asp?name1=value1&name2=value2
Some other notes on GET requests:
GET requests can be cached
GET requests remain in the browser history
GET requests can be bookmarked
GET requests should never be used when dealing with sensitive data
GET requests have length restrictions
GET requests should be used only to retrieve data
I am facing very strange issue here. I have a servlet running on my machine which renders my web page based on some input parameters.
Now, my screen capture with PhantomJS is not working if I try to send my data as a JSON object as a POST request type. For e.g. if I try:
Client side
var data = {"op": "get"};
page.open(address, 'POST', data, function (status) {
if (status !== 'success') {
console.log('[ERROR] :: Unable to fetch the address - ' + address + ", with data - " + data);
phantom.exit();
} else {
page.render(output);
}
console.log('processing...');
});
Server side
Now, on the server side, I am using Apache Velocity View templating so I have a single method which handles both get and post like :
public Template handleRequest(HttpServletRequest request, HttpServletResponse response,
Context context){
System.out.println(request.getParameter("op"));
//Always null
}
However, if I try sending my data from phantomjs as:
var data = "op=get&..."
It works
Also, at many places elsewhere in my code..I am doing Ajax POST requests to the same servlet and it works perfectly for all those request.
Would anybody explain why my servlet is not reading the JSON parameters passed from Phantomjs?
Servlets deal with simple request, so they only know how to parse (natively) HTTP parameters, either GET parameters from the URL, or POST parameters sent as application/x-www-form-urlencoded. Newer versions of the Servlet specification can also read multipart/form-data.
But there's nothing about JSON mentioned either in the Servlet or the HTTP specifications. So you must use a library that knows how to parse JSON and make the resulting object available in the Velocity context.