Web API Examples — SOAP, REST, GraphQL, gRPC
Below are simplified examples for SOAP, REST, GraphQL, and gRPC requests and responses. These examples use a hypothetical scenario where a client is requesting details about a user with an ID of 123.
SOAP Request and Response
Section titled “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.1Host: example.comContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "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 OKContent-Type: text/xml; charset=utf-8Content-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
Section titled “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.1Host: example.comAccept: application/jsonREST Response:
HTTP/1.1 200 OKContent-Type: application/json
{ "userID": "123", "name": "John Doe", "email": "john.doe@example.com"}GraphQL Request and Response
Section titled “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.1Host: example.comContent-Type: application/jsonContent-Length: length
{ "query": "query GetUserDetails($userID: ID!) { user(id: $userID) { id name email } }", "variables": { "userID": "123" }}GraphQL Response:
HTTP/1.1 200 OKContent-Type: application/json
{ "data": { "user": { "id": "123", "name": "John Doe", "email": "john.doe@example.com" } }}gRPC Request and Response
Section titled “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 serviceservice UserService {rpc GetUserDetails(UserRequest) returns (UserResponse) {}}
// Define request messagemessage UserRequest { string user_id = 1;}
// Define response messagemessage 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.