Implementation:A616567126 GPT WEB JAVA BardUtils
| Knowledge Sources | |
|---|---|
| Domains | Bard_Integration, HTTP_Request_Building |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete utility class for building Google Bard HTTP requests, parsing Bard API responses, and extracting session tokens and conversation state from response data.
Description
The BardUtils class provides static methods for: building OkHttp requests with Bard-specific headers (cookies, user-agent, origin), extracting the `SNlM0e` session token from HTML, generating query parameters for ask requests, building form-encoded POST bodies, and parsing the complex nested JSON response into `BardResponse` objects with conversation ID, response ID, choice ID, answer text, and images.
Usage
Use this utility when integrating with Google Bard's internal web API, particularly for request building and response parsing.
Code Reference
Source Location
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/utils/bard/BardUtils.java
- Lines: 1-183
Signature
public class BardUtils {
public static Request.Builder createBuilderWithBardHeader(String token);
public static Request createRequestForSNlM0e(String token);
public static String fetchSNlM0eFromBody(String input);
public static Request createPostRequestForAsk(String token, BardRequest bardRequest);
public static RequestBody buildRequestBodyForAsk(BardRequest bardRequest);
public static BardResponse renderBardResponseFromResponse(String content);
public static String removeBackslash(String answerStr);
}
Import
import com.intelligent.bot.utils.bard.BardUtils;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| token | String | Yes | Two cookie values (1PSID;1PSIDTS) semicolon-separated |
| bardRequest | BardRequest | Yes (ask) | Question and conversation state |
| content | String | Yes (parse) | Raw Bard API response string |
| input | String | Yes (SNlM0e) | HTML body from Bard homepage |
Outputs
| Name | Type | Description |
|---|---|---|
| createRequestForSNlM0e | Request | OkHttp request for fetching SNlM0e |
| fetchSNlM0eFromBody | String | Extracted SNlM0e token or null |
| createPostRequestForAsk | Request | OkHttp POST request for asking question |
| renderBardResponseFromResponse | BardResponse | Parsed response with answer and state |
Usage Examples
Extract Session Token
// Fetch Bard homepage and extract SNlM0e
Request request = BardUtils.createRequestForSNlM0e(token);
Response response = httpClient.newCall(request).execute();
String body = response.body().string();
String snlM0e = BardUtils.fetchSNlM0eFromBody(body);
Parse Bard Response
// Parse the complex nested JSON response
BardResponse bardResponse = BardUtils.renderBardResponseFromResponse(rawResponseString);
String answer = bardResponse.getAnswer().getChosenAnswer();
String conversationId = bardResponse.getConversationId();