Call JS through cURL - javascript

I am trying to call my js file through cURL. That JS file in-turn call's third party API to fetch me the result.
Overview of JS file.
In js file i am creating an object of the 3rd party library file and sending them the relevant data to get the response back. This response need to be captured and returned back when call'd through cURL.
Procedure of calling cURL
The cURL function will be triggered as an API call.
What i have tried so far !
I am storing the response of the library file in a cookie. So that i can use the same. I am able to achieve this through browser. But, when tried with REST client, i get all my <script> function as response also it doesn't call the library file too.
So, how can i call the JS file through cURL OR is there any other alternative way to achieve this. ?
NOTE : It is not a regular browser flow, it has to be a API call.
Please let me know on this !

Related

can AJAX do anything else than load a JSON file?

I'm want to (or think I need to) use AJAX to accomplish what I intend.
When clicking on a specific link in a list of links, I want to fill the HTML markup below with content of specific subpages. The data is naturally somewhere in the database and actually easily accessible with the CMS's API (I'm using Processwire).
I'm quite new to coding and especially AJAX and all documentation I find online only mention it in combination with a JSON file that would be loaded via AJAX.
However, I don't have a JSON file on the server, that means, according to my understanding, I would need to
store the data I need in a multidimensional php array,
use json_decode to create and then save that JSON-file on the server,
load that file via AJAX and process through more JS.
Let alone keep that JSON-file updated (or create a new one and delete the old one?) since new content will arrive periodically. It seems unnecessarily complicated to me, but what do I know.
There's got to be a better way…
Any help is appreciated.
AJAX is simply a way to make a request to the web server for information.
When you make an AJAX request you ask for a response from a file on a server. So, you can send an AJAX request to a PHP script for-instance.
The PHP script could return anything, JSON is common and very widely used response format, but XML might be another one you've encountered.
So, your request for information is made using AJAX, and the response you get back is JSON.
You don't need to store a JSON file on your server. You just need to make an AJAX request that returns current data in JSON format.
AJAX allows you to do asynchronous HTTP requests.
You can of course ask for a json file, but you can also (for example) call an API.
I suggest you start by reading the the getting started guide for AJAX in MDN:
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

In Marklogic, I have a custom JavaScript function. How do I call it through REST API? What is the process of calling it from CURL?

I have a few documents in my database and I've designed search functions for them. So I want to use curl in cmd and call the function so that I can run the searches and print the results in cmd only.
For example, I have a function where I'm passing the "userID" and I want all relevant documents. So I need to pass that value in the code or can I specify this(params) in curl.
If not curl, what other REST API options do we have to call custom JavaScript functions?
One approach is to create a REST API server and use either the invoke or resource service extension endpoints to invoke your script.
For information about standing up the REST API, see:
http://docs.marklogic.com/guide/rest-dev/intro#id_97899
For information about invoking code, see:
http://docs.marklogic.com/guide/rest-dev/extensions#id_72813
For information about a resource service extension (which requires that your code conform to conventions), see:
http://docs.marklogic.com/guide/rest-dev/extensions#id_21018
Hoping that helps,

How to extract a JavaScript fetched information with Requests module?

I need two informations to generate the url to send the POST request to the server: api_token and cid, as you can see in the JS code bellow:
// Run query
fetch(`/ajax/gw-light.php?api_version=1.0&api_token=${encodeURIComponent(config.get('checkForm'))}&input=3&cid=${cid || id}`
I can get the token by parsing the HTML, but the 'cid' is fetched by functions in JavaScript. Is there any way to get this only by Requests?
I know about Selenium, but its very very slow. Requests goes directly to the point.
You cannot do this with Python Requests, you will need to look to use a headless browser as Python Requests does not execute JavaScript. You may be able to see where this code is being executed, for example... if there is an api call being made to get this token, than you could replicate it with Python Requests.
The bottom line is Python Requests and BeautifulSoup do not execute JavaScript it's simply HTTP and not a browser JS engine.

sending local mp3 file to api server using jquery

I wish to use echoprint - http://echoprint.me/start - which allows me to send an mp3 file locally from my computer in a post request, and returns a json object including the song's details from their server.
I am attempting to make this post request using jquery in order to allow me retrieve the json object containing the song details, which will then allow me view this in my browser's console.
The echoprint website - http://developer.echonest.com/docs/v4/track.html - explains how to make this post request using curl. The following code works in the command line. This returns a json object, however this gets returned in the terminal.
curl -F "api_key=#############" -F "filetype=mp3" -F "track=#tambourineMan.mp3" "http://developer.echonest.com/api/v4/track/upload"
I have read the curl docs http://curl.haxx.se/docs/httpscripting.html#POST to try and understand where the correlation exists between the curl and jquery, but unfortunately I am having difficulties relating the two and understanding what -F means.
My aim is to make this post request using jquery so I can make the same request as outlined using curl above, and retrieve the json data in the browser's console.
From a previous question I asked on here I have tried to adopt the logic from that answer and used the following code, however this returns an error that the file cannot be encoded. I have tried it with and without the content type specified, but both methods fail.
$.post("http://developer.echonest.com/api/v4/track/upload", {
"api_key":"##################",
"track":"#tambourineMan.mp3",
"filetype":"mp3",
"contentType:" "application/octet-stream"
},
function( data ) {
console.log(data)
},
"JSON" );
There are instructions here http://developer.echonest.com/docs/v4/track.html but they only explain how to do this using curl. If anyone could shed any light on this it would be greatly appreciated. Pardon my ignorance in advance.
cURL uses the # prefix to mean "the contents of the named file", in your AJAX request you are sending #tambourineMan.mp3 as a literal string.
One easy to way to accomplish your task is to put a file input in your document and tell jQuery to use the data from that file:
var file = document.getElementById('myFileInput').files[0];
$.post("http://developer.echonest.com/api/v4/track/upload", {
"api_key":"##################",
"track":file,
"filetype":"mp3",
"contentType:" "application/octet-stream"
});
Take a look at the FileReader API and at this article about sending and receiving binary data in a XMLHttpRequest

Read/write json from js to file on server (not server app)

I'm working with a .js client and have and object that I need to write out to a file on the server. A couple of questions - file i/o with JavaScript is new to me... I was planning on using jquery and json. I'm using java serverside. I don't have a problem reading what I get back from my servlet, but the file i/o is killing me! A couple of questions:
I can open a file I generated myself via the .js with an $.ajax call, but it's not handling my json syntax (I tried both an $.getJson and $.ajax - handwritten json, so I might (probably) are doing something wrong with it). I used firebug's console and it looks ok...
How can I write my object to a file on the server?
Then, when I want to read it, what do I need to do to process it? Right now I'm using a jsonFilter function (uses JSON.parse if that's available, otherwise eval) to process data that I'm getting from the servlet.
The object I'm writing isn't simple, but it's not super complex either. There's an array that contains an array, but that shouldn't make a difference if the software is both reading/writing it.
Thanks for any help! I'm at a loss - tried alot of different things.
You can open a file located on the server via ajax by querying the file and loading it into a JSON object. You might want to LINT your JSON
You can not write to an object on the server via the client. This is a severe security breach.
Common practice is to change the JSON data and then send it via ajax to server-side code. The server will then do the file IO.
Yes using JSON.parse otherwise eval is indeed correct. I would recommend json2.js
The data should be fine as long as it passes JSONLint.
Your main issue is that it's impossible to write to the server from the client. Get the client to load the data through ajax change it and then query the server to update the file.
js don't have i/o property;
you should use ajax or http request to send message to server,and tell server to do de i/o action...

Categories

Resources