F
F
fsgdoterr2022-01-18 22:37:05
PHP
fsgdoterr, 2022-01-18 22:37:05

Why do we need methods for sending data other than GET, POST?

Why do we need other requests like OPTIONS, PUT, PATCH, DELETE, HEAD, TRACE, CONNECT, if in html forms, js ajax they cannot be sent (and you have to invent bicycles by the type of a hidden field with the value of the name of one of these methods), and in php cannot process them (only if, again, to accept them in post, just check the value of the received variable, say method). And does anyone know if there will be normal support for these methods in the same php and html forms? it’s also interesting what’s the point of using them at all if in the end you can do the same thing with a regular post as without them

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey delphinpro, 2022-01-19
@fsgdoterr

It is convenient to make endpoints in API

[GET]    /order/{id}  получить информацию о заказе
[POST]   /order/{id}  создать новый заказ
[PUT]    /order/{id}  обновить данные в заказе
[DELETE] /order/{id}  удалить заказ

Instead of
[GET]  /order/{id}
[POST] /order/{id}/create
[POST] /order/{id}/update
[POST] /order/{id}/delete

will there be normal support for these methods in the same php and html forms?

https://caniuse.com/mdn-http_methods_put
https://caniuse.com/mdn-http_methods_delete
What do you mean by normal in php? You can determine the request method by reading $_SERVER['REQUEST_METHOD'], get data from php://input
UPD
Found a similar question. The answers are short but to the point.
https://stackoverflow.com/questions/27941207/http-...

T
Timur Maslov, 2022-01-19
@tmaslov22

Read the HTTP specification, they describe why such methods are used, etc. In addition to html and php, there are a lot of technologies in which HTTP is used completely.

B
bro-dev, 2022-01-20
@xPomaHx

You can send everything, even any custom methods, thereby violating the specification.

spoiler
[
    'OPTIONS', 'post', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'TRACE', 'CONNECT', 'hui'
].forEach(method => {
    try {
        const req = new XMLHttpRequest()
        req.onload = console.dir
        req.open(method, '/')
        req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

        req.send(JSON.stringify({
            method
        }))
    } catch (er) {
        console.error('disallow', method)
    }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question