Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send a body on delete method #206

Closed
lubritto opened this issue Oct 9, 2018 · 15 comments
Closed

Send a body on delete method #206

lubritto opened this issue Oct 9, 2018 · 15 comments

Comments

@lubritto
Copy link

lubritto commented Oct 9, 2018

The method http.delete just accept url and headers, can you put a body parameter ?

@natebosch
Copy link
Member

A body isn't specifically disallowed by the spec but also has no specced semantics. I'm not sure if we'd add one or not.

What is the motivation?

@Henge9
Copy link

Henge9 commented Oct 25, 2018

I would like to have the body in the DELETE method because people don't read the standard, and the standard is vague.

OpenAPI Specification ver. 2 in swagger allows it. (At least in javascript and go)

Its probably wrong but it works.

@rxwen
Copy link

rxwen commented Oct 30, 2018

+1

@ghost
Copy link

ghost commented Oct 30, 2018

For reference, RFC 2616 doesn't mention much in connection with DELETE, but RFC 7231 has the following to say:

A payload within a DELETE request message has no defined semantics;
sending a payload body on a DELETE request might cause some existing
implementations to reject the request.

So while setting body on a DELETE request isn't strictly forbidden in the spec, it is discouraged.
Unless there is a really good use case for allowing it, it does not seem like a worthwhile investment to add it.
The fact that other libraries allow it in itself is not a persuasive argument.

@hacker1024
Copy link

Is it really our job to decide whether users are allowed to do things discouraged by the spec? A HTTP library should be able to do anything allowed by the spec, whether it's discouraged or not. This is a missing feature.

@thosakwe
Copy link

You can do this:

var rq = Request('DELETE', Uri.parse(...));
rq.bodyFields = {...};

var response = await client.send(rq).then(Response.fromStream);

It's not a "missing feature," IMO, because it's already supported. get, post, delete, etc. are just shorthand methods in the BaseClient class...

@quetool
Copy link

quetool commented Apr 17, 2019

@thosakwe I am trying to implement your suggestion but it seems like I can not add headers parameters with that, is it possible?

@quetool
Copy link

quetool commented Apr 18, 2019

I did it, I paste the code if it helps someone

(DELETE particular token from server)

http.Request rq = http.Request('DELETE', Uri.parse('${_getUrl()}/push.token'))
  ..headers.addAll({
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-User-Id': _auth._id,
    'X-Auth-Token': _auth._token,
  });
rq.bodyFields = {
  'token': token,
};

http.StreamedResponse response = await http.Client().send(rq);

@devmvk
Copy link

devmvk commented May 12, 2019

Please add support for body parameter in http.delete method

@thosakwe
Copy link

thosakwe commented May 12, 2019 via email

@dfmiller
Copy link

Use case: Deleting a resource does not necessarily mean it is actually being deleted. It may be being marked deleted in the database and may require a reason for the deletion.

@DartMen
Copy link

DartMen commented Feb 22, 2020

Ive also encountered badly designed API's which needs payloads in DELETE methods and had to implement some boilerplate to make this work.

Would love to see an optional overridable method to feed a payload.

@danangponorogo
Copy link

danangponorogo commented Feb 24, 2020

Hi all, I've just found simple code to get call back value after delete with RESTful endpoint, still can't go with http.delete, but it can do RESTful delete as expected with id parameter on body. thanks to @quetool for the inspiration. Here's the code:

`// Flutter code

void deleteData() async {
http.Request rq = http.Request('DELETE', Uri.parse(widget.url));
rq.bodyFields = {
'id': widget.list[widget.index]['id'],
};
await http.Client().send(rq).then((response) {
response.stream.bytesToString().then((value) {
print(value); // it will print: {"status":"Success"}
});
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => Home(),
));
});
}`

`// PHP RESTfull code

function index_delete() // Delete
{
$id = $this->delete('id');
$delete = $this->model->delete($id);
if ($delete) {
$this->response(array('status' => 'Success'), 201);
} else {
$this->response(array('status' => 'Failed', 304));
}
}`

idabgsram pushed a commit to idabgsram/flutter_http_legacy that referenced this issue Jun 2, 2021
The spec discourages but does not forbid a body. Allow a body argument
without falling back on the `send` API since some servers may be
designed specifically to require it.

Closes dart-lang#383, closes dart-lang#206
@cv-irvan
Copy link

I did it, I paste the code if it helps someone

(DELETE particular token from server)

http.Request rq = http.Request('DELETE', Uri.parse('${_getUrl()}/push.token'))
  ..headers.addAll({
    'Content-Type': 'application/x-www-form-urlencoded',
    'X-User-Id': _auth._id,
    'X-Auth-Token': _auth._token,
  });
rq.bodyFields = {
  'token': token,
};

http.StreamedResponse response = await http.Client().send(rq);

how to get the body from response?

@thosakwe
Copy link

@cv-irvan Look at the last line of my solution above. Basically, you use http.Response.fromStream() to convert a StreamedResponse into a Response. And then you can fetch the body or bodyBytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests