Third-party clients#
This page is about accessing the Flow with other clients, e.g. curl
, or programming languages other than Python.
Mostly developed for docarray<0.30
Note that most of these clients have been developed for versions of Jina compatible with docarray<0.30.0
. This means, they will only be able to communicate with services
using Jina with docarray<0.30.0
Golang#
Our Go Client supports gRPC, HTTP and WebSocket protocols, allowing you to connect to Jina from your Go applications.
PHP#
A big thanks to our community member Jonathan Rowley for developing a PHP client for Jina!
Kotlin#
A big thanks to our community member Peter Willemsen for developing a Kotlin client for Jina!
HTTP#
Available Protocols
Jina Flows can use one of three protocols: gRPC, HTTP, or WebSocket. Only Flows that use HTTP can be accessed via the methods described below.
Apart from using the Jina Client, the most common way of interacting with your deployed Flow is via HTTP.
You can always use post
to interact with a Flow, using the /post
HTTP endpoint.
With the help of OpenAPI schema, one can send data requests to a Flow via cURL
, JavaScript, Postman, or any other HTTP client or programming library.
Arguments#
Your HTTP request can include the following parameters:
Name |
Required |
Description |
Example |
---|---|---|---|
|
required |
Executor endpoint to target |
|
|
optional |
List specifying the input Documents |
|
|
optional |
Dictionary of parameters to be sent to the Executors |
|
|
optional |
String indicating an Executor to target. Default targets all Executors |
|
Instead of using the generic /post
endpoint, you can directly use endpoints like /index
or /search
to perform a specific operation.
In this case your data request is sent to the corresponding Executor endpoint, so you don’t need to specify the parameter execEndpoint
.
Example
curl --request POST \
'http://localhost:12345/search' \
--header 'Content-Type: application/json' -d '{"data": [{"text": "hello world"}]}'
fetch(
'http://localhost:12345/search',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({"data": [{"text": "hello world"}]})
}).then(response => response.json()).then(data => console.log(data));
The response you receive includes data
(an array of Documents), as well as the fields routes
, parameters
, and header
.
See also: Flow REST API
For a more detailed description of the REST API of a generic Flow, including the complete request body schema and request samples, please check:
For a specific deployed Flow, you can get the same overview by accessing the /redoc
endpoint.
Use cURL#
Here’s an example that uses cURL
:
curl --request POST 'http://localhost:12345/post' --header 'Content-Type: application/json' -d '{"data": [{"text": "hello world"}],"execEndpoint": "/search"}'
Sample response
{
"requestId": "e2978837-e5cb-45c6-a36d-588cf9b24309",
"data": {
"docs": [
{
"id": "84d9538e-f5be-11eb-8383-c7034ef3edd4",
"granularity": 0,
"adjacency": 0,
"parentId": "",
"text": "hello world",
"chunks": [],
"weight": 0.0,
"matches": [],
"mimeType": "",
"tags": {
"mimeType": "",
"parentId": ""
},
"location": [],
"offset": 0,
"embedding": null,
"scores": {},
"modality": "",
"evaluations": {}
}
],
"groundtruths": []
},
"header": {
"execEndpoint": "/index",
"targetPeapod": "",
"noPropagate": false
},
"parameters": {},
"routes": [
{
"pod": "gateway",
"podId": "5742d5dd-43f1-451f-88e7-ece0588b7557",
"startTime": "2021-08-05T07:26:58.636258+00:00",
"endTime": "2021-08-05T07:26:58.636910+00:00",
"status": null
}
],
"status": {
"code": 0,
"description": "",
"exception": null
}
}
Use JavaScript#
Sending a request from the front-end JavaScript code is a common use case too. Here’s how this looks:
fetch('http://localhost:12345/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({"data": [{"text": "hello world"}],"execEndpoint": "/search"})
}).then(response => response.json()).then(data => console.log(data));
Output
{
"data": [
{
"id": "37e6f1bc7ec82fc4ba75691315ae54a6",
"text": "hello world"
"matches": ...
},
"header": {
"requestId": "c725217aa7714de88039866fb5aa93d2",
"execEndpoint": "/index",
"targetExecutor": ""
},
"routes": [
{
"executor": "gateway",
"startTime": "2022-04-01T13:11:57.992497+00:00",
"endTime": "2022-04-01T13:11:57.997802+00:00"
},
{
"executor": "executor0",
"startTime": "2022-04-01T13:11:57.993686+00:00",
"endTime": "2022-04-01T13:11:57.997274+00:00"
}
],
]
}
Use Swagger UI#
Flows provide a customized Swagger UI which you can use to visually interact with the Flow through a web browser.
Available Protocols
Only Flows that have enabled CORS expose the Swagger UI interface.
For a Flow that is exposed on port PORT
, you can navigate to the Swagger UI at http://localhost:PORT/docs
:
Here you can see all the endpoints that are exposed by the Flow, such as /search
and /index
.
To send a request, click on the endpoint you want to target, then Try it out
.
Now you can enter your HTTP request, and send it by clicking Execute
.
You can again use the REST HTTP request schema, but do not need to specify execEndpoint
.
Below, in Responses
, you can see the reply, together with a visual representation of the returned Documents.
Use Postman#
Postman is an application that allows the testing of web APIs from a graphical interface. You can store all the templates for your REST APIs in it, using Collections.
We provide a suite of templates for Jina Flow. You can import it in Postman in Collections, with the Import button. It provides templates for the main operations. You need to create an Environment to define the {{url}}
and {{port}}
environment variables. These would be the hostname and the port where the Flow is listening.
This contribution was made by Jonathan Rowley, in our community Slack.
gRPC#
To use the gRPC protocol with a language other than Python you will need to:
Download the two proto definition files:
jina.proto
anddocarray.proto
from GitHub (be sure to use the latest release branch)Compile them with protoc and specify which programming language you want to compile them with.
Add the generated files to your project and import them into your code.
You should finally be able to communicate with your Flow using the gRPC protocol. You can find more information on the gRPC
message
and service
that you can use to communicate in the Protobuf documentation.
GraphQL#
See Also
This article does not serve as the introduction to GraphQL. If you are not already familiar with GraphQL, we recommend you learn more about GraphQL from the official documentation. You may also want to learn about Strawberry, the library that powers Jina’s GraphQL support.
Jina Flows that use the HTTP protocol can also provide a GraphQL API, which is located behind the /graphql
endpoint.
GraphQL has the advantage of letting you define your own response schema, which means that only the fields you require
are sent over the wire.
This is especially useful when you don’t need potentially large fields, like image tensors.
You can access the Flow from any GraphQL client, like sgqlc
.
from sgqlc.endpoint.http import HTTPEndpoint
HOSTNAME, PORT = ...
endpoint = HTTPEndpoint(url=f'{HOSTNAME}:{PORT}/graphql')
mut = '''
mutation {
docs(data: {text: "abcd"}) {
id
matches {
embedding
}
}
}
'''
response = endpoint(mut)
WebSocket#
WebSocket uses persistent connections between the client and Flow, hence allowing streaming use cases. While you can always use the Python client to stream requests like any other protocol, WebSocket allows streaming JSON from anywhere (CLI / Postman / any other programming language). You can use the same set of arguments as HTTP in the payload.
We use subprotocols to separate streaming JSON vs bytes.
The Flow defaults to json
if you don’t specify a sub-protocol while establishing the connection (Our Python client uses bytes
streaming by using jina.proto definition).
Hint
Choose WebSocket over HTTP if you want to stream requests.
Choose WebSocket over gRPC if
you want to stream using JSON, not bytes.
your client language doesn’t support gRPC.
you don’t want to compile the Protobuf definitions for your gRPC client.