0

I have the following input field:

 <input id="dataRequest" type="text" method="POST" border=None value=""/>

and developed code that responds to the following cURL command.

curl -v --insecure -H "Content-Type: application/x-www-form-urlencoded" -X POST -d 'dataRequest=demo1' https://localhost:443

Entering a value into the input field does not work.

Question 1: Why does the cURL code work when filling in the field does not?

Question 2: I am trying to use Javascript to create a button that will submit the equivalent command (to cURL) when pressed.

This is what I have so far, but it does not work (nothing happens).

<button onclick="runDemo1()" name="button1" value="demo1">

<script>
function runDemo1() {
  let formData = new FormData();
  formData.append("dataRequest", "demo1");
  let xhr = new XMLHttpRequest();
  let page = window.location.host;
  xhr.open("POST", page, true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send(formData);
}
</script>
4
  • There's more involved in using XMLHttpRequest. The --insecure flag isn't convertable to Javascript. Commented Nov 7, 2020 at 1:45
  • reqbin.com/req/c-w7oitglz/convert-curl-to-http-request There is a website that you can maybe try. Commented Nov 7, 2020 at 2:45
  • xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log(xhr.status); console.log(xhr.responseText); }}; var data = "dataRequest=demo1"; xhr.send(data); Commented Nov 7, 2020 at 2:46
  • If you can use packages, might want to check axios (github.com/axios/axios) or ky (github.com/sindresorhus/ky). Makes things a lot easier. Commented Nov 7, 2020 at 2:51

1 Answer 1

1

I took your curl command and put it into this website: https://reqbin.com/req/c-w7oitglz/convert-curl-to-http-request

You can change the url from google to your localhost

var url = "https://google.com";

var xhr = new XMLHttpRequest();
xhr.open("POST", url);

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

var data = "dataRequest=demo1";

xhr.send(data);

Sign up to request clarification or add additional context in comments.

1 Comment

Grant, Thank you! I have learned a lot from this. Unfortunately, it does not work in my case. I think this is because of the differences between http: and https: (the --insecure tag in the cURL command) But that is a different question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.