Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I appreciate this is a complex module and that its use cases are close to infinite, so I might be wrong about this, but why are we sending data on REST services using GET arguments? That's just too limiting and I am really not sure what's the use case for.
Further more, there is already a way to send named arguments on the URL with the @replacement as per
// Replace argument patterns in the operation URL.
foreach ($arguments as $key => $value) {
if (strpos($operation_url, '@' . $key) !== FALSE) {
$operation_url = str_replace('@' . $key, $value, $operation_url);
unset($arguments[$key]);
}
}
I am proposing to send the data directly in the content part of the request than in the URL argument.
Comment | File | Size | Author |
---|---|---|---|
#13 | 2426053-13-send-as-data-post-put.patch | 703 bytes | rumenyordanov |
#12 | 2426053-12-send-as-data-post-put.patch | 702 bytes | rumenyordanov |
#11 | 2426053-11-send-as-data-post-put.patch | 648 bytes | skek |
#10 | 2426053-10-send-as-data-post-put.patch | 519 bytes | skek |
#4 | 2426053-4-send-as-data-post-put.patch | 891 bytes | hanoii |
Comments
Comment #1
hanoiiComment #2
hanoiiComment #3
dman CreditAttribution: dman commentedWhile it may be possible that someone has built a REST server that expects it's parameters to be sent as content... That's not the way I've usually seen GET requests work at all.
Pretty much every REST service I've seen documented places its arguments in the path, and/or as GET arguments. I've not seen any spec that does it the way you suggest, but if you can refer to such docs, there may be possibilities for it to be supported also.
What is it that makes you claim "REST server should send data within the http content"? Where did you find that idea? I can't remember seeing anything like that in any spec or documentation
Here for example is a simple public REST service. Currently, it works. (test it using a city name). With your patch, it no longer works.
If you see docs and tutorials, they usually pass parameters in this way, and that has also been true of the real-world web services I've plugged in to.
http://MyService/Persons/1?format=xml&encoding=UTF8
http://www.acme.com/phonebook/UserDetails?firstName=John&lastName=Doe
http://md5.jsontest.com/?text=convert%20this
http://jsonplaceholder.typicode.com/posts?userId=1
... your patch breaks these examples.
Until they added extra authentication, the Twitter search, and Facebook opengraph also do thishttps://graph.facebook.com/bgolub?fields=id%2Cname%2Cpicture
If you can provide working public test cases and docs for the protocol that does it your way, it's possible that this could be supported as well, but right now, what you are proposing would break all the current REST services that *do* work with GET arguments.
Comment #4
hanoiiI think I might have jumped too quickly to this assumption, basically I am not bothered by GET request.
My assumption was that when you actually need params in the GET request, you could still do so by doing something like (for your example):
This is currently supported like this in the REST module. But I see how my patch can break currently configured working web services.
I got to this patch while needing to POST/PUT data in which, I believe, you normally sends information through the DATA part of the request, see:
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/feed (the publishing part) as an example.
What about the following patch, in which you sends the arguments on the DATA part of the post for POST/PUT entries, but leave the current logic for GET and possible DELETE, I am unsure of the last one but for now at least POST/PUT would work.
Comment #5
hanoiiComment #6
dman CreditAttribution: dman commentedThat makes more sense.
If you are talking about POST not working right for you, then yeah, it's probable that that hasn't been getting fully supported until now.
TBH, I've not used POST with any live REST service, because
A: most of the time I'm just consuming data
B: if things are getting tricky and transactional, it has probably become a job for SOAP.
... but still, REST+POST should indeed be supported properly.
Although technically legal, I've not found a sane real-world service that required both GET and POST in the same action, so I think that your logic in #4 for switching the argument encoding depending on GET/POST is totally reasonable.
I'm not keen on introducing placeholder weirdness to the URL strings. That gets odd, you have to deal with the difference between param= {empty string} and param being undefined. Plus possible multiples or structured params (rare). Plus string encoding issues (common)
Let's not mess with that idea.
But this second patch has legs.
Can you point us at a public web service that demonstrates its use so it can become testable?
Comment #7
hanoiiThe placeholder weirdness is already there in the code, my point was that as that was there, you could use it for GET request, but this approach still cope with both ways.
Let me see if I find something, normally a public service won't allow you to post anything to it, you usually post something when you need to modify/create something and for that you usually needs authentication.
Comment #8
georgir CreditAttribution: georgir commentedComment #9
georgir CreditAttribution: georgir commentedIt seems you could define a single parameter (potentially of a compound type) and set $operation['data'] = '[name of your parameter]' to get this effect without a patch. But it would indeed complicate invoking the service with having to wrap the parameters in one more array().
Maybe we can use some special value of $operation['data'], such as empty string, to trigger using all the remaining parameters as data, instead of defaulting to it? Or default to it, and use a special value to trigger the old functionality, with leaving $arguments as is?
The current patch just completely removes $operation['data'] functionality from POST and PUT requests, which does not seem consistent.
And slightly related to this, we'd also need to add $operation['data'] to the UI.
Comment #10
skek CreditAttribution: skek commentedBased on the latest development version of this module, I've created a patch that will handled also the PUT requests.
Comment #11
skek CreditAttribution: skek commentedRemoving the arguments after passing to $data because of duplications.
Comment #12
rumenyordanov CreditAttribution: rumenyordanov commentedAdding unset of data if empty as it causes issues with some rest servers
Comment #13
rumenyordanov CreditAttribution: rumenyordanov commentedAdding new patch as the last one was broken
Comment #15
dman CreditAttribution: dman as a volunteer and at Sparks Interactive commentedSeems helpful and safe.
I can't test without a detailed use case that demonstrates PUT in action etc, but I don't see it breaking.
REPAIRED for code style and committed. Thanks folks!