unity3d webrequest中的Python web请求

2024-09-30 06:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

这是python关于web请求的代码。我们可以用Unity3D webrequest编写同样的python代码吗

代码

import requests
r = requests.post(
    "https://api.deepai.org/api/torch-srgan",
    files={
        'image': open('/path/to/your/file.jpg', 'rb'),
    },
    headers={'api-key': '535d7326-c37f6105b0'}
)

print(r.json())

Tags: 代码httpsorgimageimportapiwebunity3d
1条回答
网友
1楼 · 发布于 2024-09-30 06:24:51

您可以使用^{}来使用接受^{}的重载,并实际使用^{}

应该是

var bytes = File.ReadAllBytes("/path/to/your/file.jpg");
var data = new List<IMultipartFormSection>();
data.Add(new MultipartFormFileSection("image", bytes, "file.jpg", "image/jpeg"));

using(var request = UnityWebRequest.Post("https://api.deepai.org/api/torch-srgan"))
{
    request.SetRequestHeader("api-key", "535d...........");

    yield return request.SendWebRequest();

    if (request.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(request.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
        Debug.Log(request.downloadHandler.text);
    }
}

然后,为了解析JSON结果,您可以使用

[Serializable]
public class Response
{
    public string id;
    public string output_url;
}

然后呢

....

var response = JsonUtility.FromJson<Response>(request.downloadHandler.text);

Debug.Log(response.output_url);

相关问题 更多 >

    热门问题