0

如何统一发送文件。我尝试通过我的代码发送。它不起作用,但是当我从邮递员发送它时,它工作正常。

File myfi = new File("/Users/xxxxxxx/Downloads/test.png");
MultipartBody request = Unirest.post("https://api.mom.io/v1/dad/parents")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .header("api-key", "xxxxxxxxxxxx")
        .queryString("to", "9189xxxxxxxxx")
        .queryString("type", "media")
        .queryString("body", "my  Platform")
        .queryString("channel", "mom")
        .queryString("from", "xxxxxxxxxxx")
        .queryString("callback_url", "https://webhook.site")
        .queryString("lang_code", "en")
        .field("media", myfi, "multipart/form-data;");
HttpResponse<String> response = request.asString();
System.out.println(response.getStatus());
System.out.println(response.getBody());
4

1 回答 1

-1
File f = new File("/path/downloads/logo.png");       
MultipartBody r = Unirest.post("https://api.mom.io/v1/dad/parents")
                        .header("api-key", "xxxxxxxxxxxx").field("to", "9189xxxxxxxxx")
                        .field("channel", "mom").field("type", "media").field("from", "xxxxxxxxxxx")
                        .field("callback_url", "https://webhook.site")
                        .field("media", f).field("body", "test message");
                HttpResponse<String> res = r.asString();
                System.out.println(res.getBody());
于 2021-06-23T15:13:02.433 回答