As far as i understand all what REST do is standartize a data sended to server by adding some headers. For example REST request can generate a line of bytes like so: POST /qwe HTTP/1.1 Host: 127.0.0.1 Connection: keep-alive
and finish it with some user input.
Now im just playing with writing my own JS server and here is my question: is there a way in JS to send some data(bytes) without this REST addings like headers/method and will it work for browsers and HTTP protocol itself?
For example instead of sending POST /qwe HTTP/1.1 Host: 127.0.0.1 Connection: keep-alive MY DATA OVER THERE!!!
just send MY DATA OVER THERE!!!
so my server can read only user data without everything else.
Iv tried to google and end up that XMLhttpRequest
and fetch
both require some CRUD method to be specified and adding some headers in request anyway.
CodePudding user response:
HTTP requests:
- Need to specify the method
- Need to specify the Host as a header (in HTTP 1.1. and newer)
- Will include some other request headers automatically when make using JS from a browser
This has nothing to do with REST. It's just how HTTP works.
A non-HTTP protocol could avoid having that. JavaScript in a browser has no mechanisms that allow making non HTTP requests.
You might want to research WebSocket which allows two way communication over a single connection … but that is a bootstrapped by HTTP so doesn't really fulfil your requirement.
CodePudding user response:
For example instead of sending
POST /qwe HTTP/1.1 Host: 127.0.0.1 Connection: keep-alive MY DATA OVER THERE!!!
just sendMY DATA OVER THERE!!!
so my server can read only user data without everything else.
I suspect you're misunderstanding what a request is, on a fundamental level. Without POST
(the method), /qwe
(the path), HTTP/1.1
(the protocol) and 127.0.0.1
(the address) there is no way for your computer to know where and how to send the data. These are necessary if you want to communicate with a server, and removing them will mean your code no longer works.
You're working with very low-level data here, which is probably not what you actually want to be doing. There are some packages which will let you ignore the how and what of the request, and focus on just the data inside it. Express might be a good place to start. You can set up a simple express server to handle requests on specific paths, and reply with data that your frontend can then use.
A REST API is a high-level concept and largely unrelated to what you're asking about.