You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从REST接口返回结果中提取sessionID?

Extracting sessionID from the non-standard REST response

Alright, let's figure out how to pull that sessionID from your REST response. The output has a quirky Java class prefix but the data we need is clearly structured, so here are a couple of solid approaches:

First, let's recap your sample response for reference:

!!br.com.return.test.rest.schema.CtresponseResp {error: null, sessionID: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13}

Approach 1: Use Regular Expressions (most reliable for UUIDs)

Since your sessionID is a standard UUID (8-4-4-4-12 character format), we can write a regex that specifically targets that pattern, or matches directly after the sessionID: key.

Example in Python:

import re

response_str = "!!br.com.return.test.rest.schema.CtresponseResp {error: null, sessionID: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13}"
# Match the UUID directly after sessionID:
match = re.search(r'sessionID: (\w{8}-\w{4}-\w{4}-\w{4}-\w{12})', response_str)
if match:
    session_id = match.group(1)
    print(session_id)  # Output: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13

Example in Java:

String response = "!!br.com.return.test.rest.schema.CtresponseResp {error: null, sessionID: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13}";
Pattern uuidPattern = Pattern.compile("sessionID: ([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})");
Matcher matcher = uuidPattern.matcher(response);

if (matcher.find()) {
    String sessionId = matcher.group(1);
    System.out.println(sessionId); // Prints the sessionID value
}

Approach 2: String Manipulation (for simpler key-value extraction)

If you prefer avoiding regex, you can split the string to isolate the key-value pairs inside the curly braces:

Example in Python:

response_str = "!!br.com.return.test.rest.schema.CtresponseResp {error: null, sessionID: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13}"

# Extract the content inside curly braces
inner_content = response_str.split('{')[1].rstrip('}').strip()
# Split into individual key-value pairs
key_value_pairs = inner_content.split(', ')

# Loop through pairs to find sessionID
session_id = None
for pair in key_value_pairs:
    key, value = pair.split(': ')
    if key == 'sessionID':
        session_id = value
        break

print(session_id)  # Output: c1d840fd-fc2a-4e04-8c21-ffb5a9a58a13

Both methods will reliably pull out the exact sessionID value you need. The regex approach is especially robust if the response format varies slightly (like extra whitespace), while string manipulation works well if the structure stays consistent.

内容的提问来源于stack exchange,提问作者Jerson Luiz de Paula Junior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:40:52