-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test.c
More file actions
55 lines (45 loc) · 1.45 KB
/
api-test.c
File metadata and controls
55 lines (45 loc) · 1.45 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define VFUN_URL "http://vfun:4380/wtf"
#define AUDIO_FILE "/data/english_phonecall.wav"
struct buffer {
char *data;
size_t len;
};
static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *userp) {
size_t total = size * nmemb;
struct buffer *b = userp;
b->data = realloc(b->data, b->len + total + 1);
memcpy(b->data + b->len, ptr, total);
b->len += total;
b->data[b->len] = '\0';
return total;
}
int main(void) {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
curl_mime *mime = curl_mime_init(curl);
curl_mimepart *part = curl_mime_addpart(mime);
curl_mime_name(part, "file-binary");
curl_mime_filedata(part, AUDIO_FILE);
struct buffer resp = {0};
curl_easy_setopt(curl, CURLOPT_URL, VFUN_URL);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl failed: %s\n", curl_easy_strerror(res));
return 1;
}
long code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
printf("HTTP %ld\n%.200s...\n", code, resp.data);
curl_mime_free(mime);
curl_easy_cleanup(curl);
free(resp.data);
curl_global_cleanup();
return code == 200 ? 0 : 1;
}