CDLUC3 / N2T

next-gen N2T resolver
MIT License
1 stars 2 forks source link

Implement POST method #34

Closed coret closed 1 month ago

coret commented 1 month ago

When I POST data to https://n2t.net/ark:/60537/aCtXvNJ I get a HTTP 405 error with contents {"detail":"Method Not Allowed"}. This seem to be triggered by fastapi in nt2/app.py

I think redirecting (with HTTP 307 to keep method) of POST requests is allowed per spec. If so, please implement POST method in resolver. It's the client responsibility to repeat the POST of data (not the resolver).

For testing purposes:

curl -L https://n2t.net/ark:/60537/aCtXvNJ \
  -H 'accept: application/x-sparqlstar-results+json, application/sparql-results+json;q=0.9, */*;q=0.8' \
  -H 'content-type: application/x-www-form-urlencoded' \
  --data-raw 'query=select%20*%20where%20%7B%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%20limit%2010&infer=true&sameAs=true&offset=0&limit=1001'  

I expected that a POST to https://n2t.net/ark:/60537/aCtXvNJ would have been redirected to https://www.goudatijdmachine.nl/ark:/60537/aCtXvNJ as the following does have the desired output:

curl -L https://www.goudatijdmachine.nl/ark:/60537/aCtXvNJ \
  -H 'accept: application/x-sparqlstar-results+json, application/sparql-results+json;q=0.9, */*;q=0.8' \
  -H 'content-type: application/x-www-form-urlencoded' \
  --data-raw 'query=select%20*%20where%20%7B%0A%20%20%20%20%3Fs%20%3Fp%20%3Fo%20.%0A%7D%20limit%2010&infer=true&sameAs=true&offset=0&limit=1001'  
datadavev commented 1 month ago

The legacy n2t implementation would redirect POST, PUT, etc with a 302 by default, which can lead to ambiguity in client behavior. HTTP status codes 307 and 308 were introduced to reconcile the ambiguity and direct a client to reuse the same method on redirected target. From the perspective of a resolver service, the intent is to redirect a client to the known location of a resource, hence it would appear correct to perform redirect for any of HEAD, GET, POST, PUT, and DELETE, with the caveat that POST, PUT, and DELETE requests should use 307 or 308 to ensure the client uses the same method on the redirected request.

Task here is to add additional HTTP method handling to support methods other than GET and HEAD.

Behavior:

datadavev commented 1 month ago

N2T and ARKs.org resolver has been updated to support redirection on HTTP POST, PUT, and DELETE methods (in addition to HEAD and GET). Deployed N2T tag v0.9.1 to stage and production environments.