Edit: moved to Upgrade HTTP request calls from `query` to `update` (upon canister's request)
I would also like to propose that my PR to upgrade HTTP request calls from query
to update
(upon canister’s request) be considered for merging, with some feedback about anything that may be blocking it.
dfinity:main
← paulyoung:paulyoung/http-request
opened 01:45AM - 28 May 21 UTC
Edit: the code in this PR no longer infers when to make an update call, it does … it upon the canister’s request.
***
This is a first pass at inferring when to make an update call instead of a query call based on HTTP request methods.
It's based on [this discussion thread](https://forum.dfinity.org/t/feature-request-map-appropriate-http-request-methods-to-update-calls/4303?u=paulyoung) in the developer forum. Thanks to @nomeata for some feedback and help getting to this point.
I think this could be made a lot more configurable (perhaps via a file that is read when `icx-proxy` starts which maps methods/routes to functions) but what's here suits my needs for now so I thought I'd share it in case there's any interest in merging.
Thanks in advance for considering this change.
***
```typescript
import Text "mo:base/Text";
actor {
type HeaderField = (Text, Text);
type Token = {};
type StreamingCallbackHttpResponse = {
body : Blob;
token : Token;
};
type StreamingStrategy = {
#Callback : {
callback : shared Token -> async StreamingCallbackHttpResponse;
token : Token;
};
};
type HttpRequest = {
method : Text;
url : Text;
headers : [HeaderField];
body : Blob;
};
type HttpResponse = {
status_code : Nat16;
headers : [HeaderField];
body : Blob;
streaming_strategy : ?StreamingStrategy;
};
public query func http_request(request : HttpRequest) : async HttpResponse {
{
status_code = 200;
headers = [];
body = Text.encodeUtf8("Response to " # request.method # " request (query)");
streaming_strategy = null;
};
};
public shared func http_request_update(request : HttpRequest) : async HttpResponse {
{
status_code = 200;
headers = [];
body = Text.encodeUtf8("Response to " # request.method # " request (update)");
streaming_strategy = null;
};
};
};
```
```
$ icx-proxy --fetch-root-key
version: 0.2.1
May 27 18:24:15.771 INFO Log Level: INFO
May 27 18:24:15.797 INFO Starting server. Listening on http://127.0.0.1:3000/
```
```
$ curl -v -X GET -H "Content-type: application/json" -H "Accept: application/json" -d '{}' 'http://localhost:3000?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Content-type: application/json
> Accept: application/json
> Content-Length: 61
>
* upload completely sent off: 61 out of 61 bytes
< HTTP/1.1 200 OK
< content-length: 31
< date: Fri, 28 May 2021 01:27:24 GMT
<
* Connection #0 to host localhost left intact
Response to GET request (query)* Closing connection 0
```
```
$ curl -v -X POST -H "Content-type: application/json" -H "Accept: application/json" -d '{}' 'http://localhost:3000?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /?canisterId=rrkah-fqaaa-aaaaa-aaaaq-cai HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.64.1
> Content-type: application/json
> Accept: application/json
> Content-Length: 61
>
* upload completely sent off: 61 out of 61 bytes
< HTTP/1.1 200 OK
< content-length: 33
< date: Fri, 28 May 2021 01:27:41 GMT
<
* Connection #0 to host localhost left intact
Response to POST request (update)* Closing connection 0
```
7 Likes