2

I'm working with a vendor who exposes an API that I can submit a POST request to with a multipart/form-data content type body. I provide three parameters and post them to the endpoint. The response is of type text/html and the body of the response is one big html page with CSS and JavaScript resources with relative file paths for these resources, like so...

<link href="/SelfServiceV3/css/site?v=UGJgWOWOr3HU-VqSuz-WTq16hygLTXMGIsn2PuMjStQ1" rel="stylesheet">

This resource will only load on the vendor's domain, not my domain where the request is made. The response has a 200 response code.

How can I consume this API through Javascript?

I'd like for users to click a button on the page and then through Javascript in the background, all of the three parameters get automatically sourced from different elements on the page, and then sent to this API which then responds with the HTML page that I want users to land on (the page returned by the API does not work in an i-frame or modal). I am unable to use any kind of redirect because I am not provided a URL by the response(other than the API endpoint that requires form data to be posted to it).

Colton Young
  • 43
  • 1
  • 4
  • Does this answer your question? [Replacing Entire Page Including Head Using Javascript](https://stackoverflow.com/questions/4292603/replacing-entire-page-including-head-using-javascript) – takendarkk Feb 24 '21 at 22:13

1 Answers1

0

How about:

  function fetchHtml(data) {
    fetch('/some/url', {
      method: 'POST',
      body: data // your FormData
    })
        .then((response) => {
          return response.text();
        })
        .then((html) => {
          document.body.innerHTML = html
        });
  }
mehowthe
  • 761
  • 6
  • 14
  • Thanks for the response. This is close, but because the javascript, css, and images on the page are referenced with a relative path (e.g. ), the page doesn't load these resources because it's trying to find them on my domain, not the vendor's domain, where the files actually live. – Colton Young Feb 25 '21 at 14:10