Support Questions

Find answers, ask questions, and share your expertise

apache livy and ajax post requests: how to send code from textarea?

Hi everyone, now I am trying to submit a code from a textarea,

I succeded to send a simple code of operations, for example 5+7, 8-7,789/0, 47*7. The problem is when the user wants to submit a huge code with many strings, how can we get that automatically and send it to the apache livy server? In other words, how can I change this line : "data":"{\"code\": \"1 + 1\"}"?

This is what I did "data": "{\"code\": \""+code+"\"}", but i works only for operations!

Thank you very much!


@Melchicédec NDUWAYO

Please try the encodeURI or encodeURIComponent function as described here and let me know how that goes:


*** If you found this answer addressed your question, please take a moment to login and click the "accept" link on the answer.

@Melchicédec NDUWAYO

Ok, what about this one?

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://c24-node2:8999/sessions/<session_id>/statements",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "X-Requested-By": "user",
    "Cache-Control": "no-cache",
    "Postman-Token": "aece2722-cbdd-48a2-ae9a-821aba97a2b6"
  "processData": false,
  "data": {code: $("#txt1").val()}

The above is using the val of the element id #txt1 you can change this to whatever id your input text box has.

Try with simple code first, once that is working you can try with the most complex cases.


Hi @Felix Albani, it doesn't work.

And I would like to tell you that I am using a variable for getting the textarea value. The problem is, in my opinion, the kind of string I can send to the apache livy. I don't if there is an equivalent of this function textwrap.dedent() in javascript like this code

from this link : Thank you

data = {
  'code': textwrap.dedent("""
    val NUM_SAMPLES = 100000;
    val count = sc.parallelize(1 to NUM_SAMPLES).map { i =>
      val x = Math.random();
      val y = Math.random();
      if (x*x + y*y < 1) 1 else 0
    }.reduce(_ + _);
    println(\"Pi is roughly \" + 4.0 * count / NUM_SAMPLES)

Thank you @Felix Albani, I will test it and let you know.

Hi @Felix Albani, I tried that function but it doesn't work also.

Thank you!

Hi @Felix Albani ,

it doesn't work. I get a bad request error (400).

Thank you

Hi everyone. By trying many examples, I saw that I can not send also many lines. It is not only the problem of quotes.

Thank you

Hi. Do you have any example using apache livy with servlet in java? How to create a session and how to send a code to apache livy by java? Thank you