0

I am working on a project where I need to intercept a "fetch" request in javascript and send it as a string to another place where I need to convert it back to a request and execute it. I tried searching online, JSON.stringify() and other Object copying & converting them to string methods doesn't work properly on all cases. Seems like the standard HTTP message format mentioned in RFC https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html is one reliable way. As my understanding is that this is how all requests are converted internally before they are physically transmitted(I may be wrong).

Example If there was a fetch request "GET" for "http://localhost:3000/jk" from an iframe in "http://localhost:3000/static/?appname=test&apphash=abc", the request object in accordance to https://developer.mozilla.org/en-US/docs/Web/API/Request will look something like this, [1]: https://i.stack.imgur.com/MAt61.png

and expected HTTP message string should be something like this(not sure),

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
........

Is there a conversion library/package available in javascript for this or is there any other reliable way to convert an entire request object in javascript(including form details, body etc...) into string in such a way that I can convert them back to request objects. I want to do the same for response objects too ie... convert them to string, transfer it to someplace, and convert back to response object.

This is my first question I apologize if I have not phrased it right or provided enough information and Thanks.

Arjun C R
  • 3
  • 2
  • Seems like that would be server side. – GetSet Aug 29 '20 at 03:01
  • Does this answer your question? [Accessing the web page's HTTP Headers in JavaScript](https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript) – kmoser Aug 29 '20 at 03:04
  • Oh, but then how are these requests physically transmitted? I mean It finally has to be a stream/chunk of bits right, what format that is in? – Arjun C R Aug 29 '20 at 03:06
  • "Does this answer your question? Accessing the web page's HTTP Headers in JavaScript " -- Nope, that is only for accessing headers – Arjun C R Aug 29 '20 at 03:07

1 Answers1

0

I don't know if there is an out-of-the-box library to achieve what you need, but as we understand how a request works is through an underlying XmlHttpRequest object, and this object has the APIs for you get all the informations about a request, for example:

To get all response headers, we can use getAllResponseHeaders, headers are returned as a single line, e.g.:

Cache-Control: max-age=31536000
Content-Length: 4260
Content-Type: image/png
Date: Sat, 08 Sep 2012 16:53:16 GMT

Here are some references for you to explore XmlHttpRequest:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

https://javascript.info/xmlhttprequest

Qiu Zhou
  • 1,235
  • 8
  • 12
  • how certain are you on the premise that the browser issues this via js even if not explicit? – GetSet Aug 29 '20 at 04:18
  • So individually gathering the headers, body etc... and converting them to string seems to be the way, but It seems like a tedious and error prone method - is that how the browser does this, thanks for the answer. – Arjun C R Aug 29 '20 at 04:24
  • "how certain are you on the premise that the browser issues this via js even if not explicit?" if this question was intended to me, well the for one thing I know that the request API has methods which are irrelevant for the server, so it sure is parsed in one way or the other before it is issued as a request by the browser - according to RFC its the format mentioned in the question. – Arjun C R Aug 29 '20 at 04:28
  • @ArjunCR I am afraid this is the best way to do it, unless someone has done it before and provided it as a library on github. And to get all request headers , it would be more tedious, cause there is no such thing like `getAllRequestHeaders` to do it in one shot , you need to get them from navigator.languages, navigator.userAgent etc. – Qiu Zhou Aug 29 '20 at 04:43
  • @GetSet XmlHttpRequest (or Ajax) is how to communicate with servers without freshing the brower, I think it's reliable. – Qiu Zhou Aug 29 '20 at 04:51
  • Its not just the headers alone, I need all the request parameters, how does the browser accomplish this, what happens to the javascript request object before the actual request is raised for the server, – Arjun C R Aug 29 '20 at 13:27
  • @ArjunCR Generally javascript is used to interact with browsers (not say nodejs or flash here), so it dosen't provide raw socket APIs, because browsers have done it for us, all we do is to tell browers all the necessary infomations for them to form a http request, like the endpoint address,GET or POST method should be used, file data to upload and etc, and this is done by creating a `XmlHttpRequest` object, then browsers compose a valid http request content according to RFC specification, – Qiu Zhou Aug 29 '20 at 15:44
  • in the first line, browsers write down `method request-target HTTP-version`, starting at second line they write down request headers as key-value pairs, after headers, leave a blank line, and then payload ( here are some real http request captured screenshots: https://linuxhint.com/http_wireshark/ ). After write down all the informations, browsers convert well formated request content into `byte array` ( since http protocal is text-based, simply use getBytes() , like in java: `content.getBytes(Charset.forName("UTF-8"));`) . – Qiu Zhou Aug 29 '20 at 15:45
  • Finally browsers send the byte array through a socket connection by calling raw socket API like `send(sockfd,pkt,sizeof(packet_size)`. – Qiu Zhou Aug 29 '20 at 15:45