Examples for various web services
Below are simplified examples for SOAP, REST, GraphQL, and gRPC requests and responses to demonstrate how each protocol operates at a basic level. These examples use a hypothetical scenario where a client is requesting details about a user with an ID of 123.
SOAP Request and Response
SOAP relies on XML for its message format. A SOAP request to get user details might look like this:
SOAP Request:
POST /UserService HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://example.com/GetUserDetails"
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:usr="http://example.com/user">
<soap:Body>
<usr:GetUserDetailsRequest>
<usr:UserID>123</usr:UserID>
</usr:GetUserDetailsRequest>
</soap:Body>
</soap:Envelope>
SOAP Response:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:usr="http://example.com/user">
<soap:Body>
<usr:GetUserDetailsResponse>
<usr:UserID>123</usr:UserID>
<usr:Name>John Doe</usr:Name>
<usr:Email>john.doe@example.com</usr:Email>
</usr:GetUserDetailsResponse>
</soap:Body>
</soap:Envelope>
REST Request and Response
RESTful services often use JSON over HTTP. Here's how a GET request for user details might look:
REST Request:
GET /users/123 HTTP/1.1
Host: example.com
Accept: application/json
REST Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"userID": "123",
"name": "John Doe",
"email": "john.doe@example.com"
}
GraphQL Request and Response
GraphQL allows clients to specify exactly what data they need. Here’s an example of how a client might request user details:
GraphQL Request:
POST /graphql HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: length
{
"query": "query GetUserDetails($userID: ID!) { user(id: $userID) { id name email } }",
"variables": {
"userID": "123"
}
}
GraphQL Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"data": {
"user": {
"id": "123",
"name": "John Doe",
"email": "john.doe@example.com"
}
}
}
gRPC Request and Response
gRPC uses protocol buffers and is designed for high-performance API calls. Here's a simplified representation of a gRPC call for getting user details:
gRPC (Protobuf Definition):
// Define a service
service UserService {
rpc GetUserDetails(UserRequest) returns (UserResponse) {}
}
// Define request message
message UserRequest {
string user_id = 1;
}
// Define response message
message UserResponse {
string user_id = 1;
string name = 2;
string email = 3;
}
gRPC Request and Response: The request and response in gRPC are not represented as plain text like HTTP/REST calls. Instead, the client and server communicate using binary protocol buffer messages defined by .proto files. When a client makes a GetUserDetails call with a UserRequest message containing the user_id "123", the server responds with a UserResponse message containing the user's details.
If you have enjoyed this post, please consider buying me a coffee ☕ to help me keep writing!