-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpost_node.py
More file actions
88 lines (71 loc) · 3.03 KB
/
post_node.py
File metadata and controls
88 lines (71 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import requests
import json
import io
class PostRequestNode:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"target_url": ("STRING", {"default": "https://example.com/api"}),
"request_body": ("STRING", {"default": "{}", "multiline": True}),
},
"optional": {
"headers": ("KEY_VALUE", {"default": None}),
"str0": ("STRING", {"default": ""}),
"str1": ("STRING", {"default": ""}),
"str2": ("STRING", {"default": ""}),
"str3": ("STRING", {"default": ""}),
"str4": ("STRING", {"default": ""}),
"str5": ("STRING", {"default": ""}),
"str6": ("STRING", {"default": ""}),
"str7": ("STRING", {"default": ""}),
"str8": ("STRING", {"default": ""}),
"str9": ("STRING", {"default": ""}),
}
}
RETURN_TYPES = ("STRING", "BYTES", "JSON", "ANY")
RETURN_NAMES = ("text", "file", "json", "any")
FUNCTION = "make_post_request"
CATEGORY = "RequestNode/Post Request"
def make_post_request(self, target_url, request_body, headers=None, str0="", str1="", str2="", str3="", str4="", str5="", str6="", str7="", str8="", str9=""):
string_inputs = {
"str0": str0, "str1": str1, "str2": str2, "str3": str3, "str4": str4,
"str5": str5, "str6": str6, "str7": str7, "str8": str8, "str9": str9
}
for key, value in string_inputs.items():
placeholder = f"__{key}__"
if value:
request_body = request_body.replace(placeholder, value)
# 嘗試解析 request_body 為 JSON
try:
body_data = json.loads(request_body)
except json.JSONDecodeError:
body_data = {"error": "Invalid JSON in request_body"}
request_headers = {'Content-Type': 'application/json'}
if headers:
request_headers.update(headers)
try:
response = requests.post(target_url, json=body_data, headers=request_headers)
# 準備四種不同格式的輸出
text_output = response.text
file_output = io.BytesIO(response.content)
try:
json_output = response.json()
except json.JSONDecodeError:
json_output = {"error": "Response is not valid JSON"}
any_output = response.content
except Exception as e:
error_message = str(e)
text_output = error_message
file_output = io.BytesIO(error_message.encode())
json_output = {"error": error_message}
any_output = error_message
return (text_output, file_output, json_output, any_output)
NODE_CLASS_MAPPINGS = {
"PostRequestNode": PostRequestNode
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PostRequestNode": "Post Request Node"
}