0

I have the following XmlHttpRequest:

var poc = new XMLHttpRequest();
poc.open("POST", URL);
poc.setRequestHeader("CUSTOM-HEADER", extract);
poc.send();
console.log("Request sent with header");

I'm wanting to submit POST body data alongside this XHR. Let's say the HTTP POST request is as follows:

POST /user/updateField/1554 HTTP/1.1
Host: localhost
Connection: close
Content-Length: 142
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarytxuXi4RTyonGC5pD

------WebKitFormBoundarytxuXi4RTyonGC5pD
Content-Disposition: form-data; name="access"

winner
------WebKitFormBoundarytxuXi4RTyonGC5pD--

How can I add this to the above JavaScript XmlHttpRequest?

If it makes it easier, how could I submit the winner value through HTTP form data via JavaScript?:

<html>
  <body>
  <script>history.pushState('', '', '/')</script>
    <form action="https://localhost:4442/user/updateField/1554" method="POST" enctype="multipart/form-data">
      <input type="hidden" name="access" value="winner" />
      <input type="submit" value="Submit request" />
    </form>
   <script>
   document.forms[0].submit();
  </script>
  </body>
</html>

Your help is much appreciated, thank you!

Sarah M.
  • 167
  • 1
  • 1
  • 12
  • 1
    Does this answer your question? [Send POST data using XMLHttpRequest](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) – Shababb Karim Mar 02 '21 at 05:31
  • [This](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) is probably what you're looking for. – codeR developR Mar 02 '21 at 05:41
  • So would something like this work? https://codeshare.io/amddQb - or is this not correct? – Sarah M. Mar 02 '21 at 05:48

0 Answers0