TensorFlow Serving C++ API Documentation
evhttp_echo_client.cc
1 /* Copyright 2018 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // A test client to print the response from the evhttp_echo_server
17 // URI: /print
18 
19 #include <iostream>
20 
21 #include "tensorflow_serving/util/net_http/client/test_client/internal/evhttp_connection.h"
22 
23 namespace {
24 
28 
29 bool SendRequest(const char* url) {
30  auto connection = TestEvHTTPConnection::Connect(url);
31  if (connection == nullptr) {
32  std::cerr << "Fail to connect to %s" << url;
33  }
34 
35  TestClientRequest request = {url, "GET", {}, ""};
36  TestClientResponse response = {};
37 
38  if (!connection->BlockingSendRequest(request, &response)) {
39  std::cerr << "Request failed.";
40  return false;
41  }
42 
43  std::cout << "Response received: " << std::endl
44  << "Status: " << static_cast<int>(response.status) << std::endl;
45 
46  for (const auto& keyval : response.headers) {
47  std::cout << keyval.first << " : " << keyval.second << std::endl;
48  }
49 
50  std::cout << std::endl << response.body << std::endl;
51  return true;
52 }
53 
54 } // namespace
55 
56 int main(int argc, char** argv) {
57  if (argc < 2) {
58  std::cerr << "Usage: http-client <url>" << std::endl;
59  return 1;
60  }
61 
62  return SendRequest(argv[1]);
63 }