-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathform_post_node.py
More file actions
79 lines (61 loc) · 2.35 KB
/
form_post_node.py
File metadata and controls
79 lines (61 loc) · 2.35 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
import requests
import json
import io
import torch
import numpy as np
from PIL import Image
class FormPostRequestNode:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"target_url": ("STRING", {"default": "http://127.0.0.1:7788/api/echo"}),
"image": ("IMAGE",),
"image_field_name": ("STRING", {"default": "image"}),
},
"optional": {
"form_fields": ("KEY_VALUE",),
"headers": ("KEY_VALUE",),
}
}
RETURN_TYPES = ("STRING", "JSON", "ANY")
RETURN_NAMES = ("text", "json", "any")
FUNCTION = "make_form_post_request"
CATEGORY = "RequestNode/Post Request"
def make_form_post_request(self, target_url, image, image_field_name, form_fields=None, headers=None):
files = []
for i, single_image in enumerate(image):
# Convert tensor to PIL Image
img_np = 255. * single_image.cpu().numpy()
img = Image.fromarray(np.clip(img_np, 0, 255).astype(np.uint8))
# Save image to a byte buffer
buffer = io.BytesIO()
img.save(buffer, format='PNG')
buffer.seek(0)
files.append((image_field_name, (f'image_{i}.png', buffer, 'image/png')))
data = form_fields if form_fields else {}
request_headers = {}
if headers:
request_headers.update(headers)
try:
response = requests.post(target_url, files=files, data=data, headers=request_headers)
text_output = response.text
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
json_output = {"error": error_message}
any_output = error_message.encode()
return (text_output, json_output, any_output)
NODE_CLASS_MAPPINGS = {
"FormPostRequestNode": FormPostRequestNode
}
NODE_DISPLAY_NAME_MAPPINGS = {
"FormPostRequestNode": "Form Post Request Node"
}