proxy-wasm-cpp-sdk icon indicating copy to clipboard operation
proxy-wasm-cpp-sdk copied to clipboard

details of httpcall method

Open kanishkarj opened this issue 5 years ago • 2 comments

I would like to make calls to an external http server via the proxy. The method 'httpcall' allows one to do it right? could someone give an example how to use it with a real URL? also, this method can be used to make calls to web service outside the cluster right (like httpbin.org for eg)?

kanishkarj avatar May 05 '20 19:05 kanishkarj

I was also trying to use the httpCall()

httpCall(StringView uri, const HeaderStringPairs& request_headers,
                                        StringView request_body,
                                        const HeaderStringPairs& request_trailers,
                                        uint32_t timeout_milliseconds, HttpCallCallback callback)

I was trying to call to an upstream cluster in the onHTTPRequestHeaders() method. But I got the following error.

em++ -s STANDALONE_WASM=1 -s EMIT_EMSCRIPTEN_METADATA=1 -s EXPORTED_FUNCTIONS=['_malloc'] --std=c++17 -O3 -flto -s WASM_OBJECT_FILES=0 --llvm-lto 1 -DPROXY_WASM_PROTOBUF_LITE=1 -I/external_sdk -I/usr/local/include --js-library /external_sdk/proxy_wasm_intrinsics.js envoy_filter_http_wasm_example.cc /external_sdk/proxy_wasm_intrinsics_lite.pb.cc /external_sdk/struct_lite.pb.cc /external_sdk/proxy_wasm_intrinsics.cc /external_sdk/libprotobuf-lite.a -o envoy_filter_http_wasm_example.wasm
envoy_filter_http_wasm_example.cc:144:37: error: call to non-static member function without an object argument
  WasmResult result2 = RootContext::httpCall(std::string("mokoon"), headers, std::string(""), headers, 10000, httpCallBack);

It would be very nice if there are some usage examples of these methods.

Thanks MenakaJ

menakaj avatar May 07 '20 10:05 menakaj

I don't have my WASM working yet. I will reply anyway, so that I don't forget.

@kanishkarj I believe the uri required by httpCall, although documented https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/blob/master/proxy_wasm_api.h#L349 as a uri, should be an Envoy cluster. Something that appears before :: in the list generated by the admin API of Envoy http://<host>:15000/clusters. I am using Istio so I am creating a ServiceEntry to expose Internet hosts like httpbin.org so that Envoy will let them be called.

@menakaj httpCall() seems to want a pointer to a function, rather than a member function. My callback is a member of my ProxyPass Context class. I am passing it using httpCall(cluster, callHeaders, ..., std::bind(&ProxyPass::proxyPassHttpCallback, this, 0, 0, 0))

I'm not quite sure why the three 0 are needed, so I may have this wrong, but it seems to do what I want -- bind an object and a member function together into a regular function.

esnible avatar Jan 29 '21 02:01 esnible