C ++请求看起来像这样
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
// case 1
// static std::vector<const char*> custom_headers = {"Token", "00000000", nullptr};
// attr.requestHeaders = custom_headers.data();
// case 2
static const char* custom_headers[3] = {"Token", "00000000-0000-0000-0000-000000000000", nullptr};
attr.requestHeaders = custom_headers;
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = onDownload;
attr.onerror = onError;
emscripten_fetch(&attr, "http://localhost:9080/api/session");
除了标题字段外,XHR工作正常 - 在两种情况下都没有设置它们。
来自emscripten_fetch_attr_t
结构的文档评论说:
// Points to an array of strings to pass custom headers to the request. This array takes the form
// {"key1", "value1", "key2", "value2", "key3", "value3", ..., 0 }; Note especially that the array
// needs to be terminated with a null pointer.
const char * const *requestHeaders;
Emscripten FetchAPI reference和code tests没有使用requestHeaders
的示例。
我究竟如何设置自己的请求标题字段?
答案 0 :(得分:1)
没有分配任何自定义标头的原因真的很愚蠢 - 它在当前版本中没有有效且经过测试的实现:
emscripten / 1.37.13 /system/lib/fetch/emscripten_fetch.cpp:308行
fetch->__attributes.requestHeaders = 0;// TODO:strdup(fetch->__attributes.requestHeaders);
作为临时解决方案,只需将其分配给自己即可
fetch->__attributes.requestHeaders = fetch->__attributes.requestHeaders
然后你将能够使用我的问题中的第二个案例:
static const char* custom_headers[3] = {"Token", "00000000-0000-0000-0000-000000000000", nullptr};
attr.requestHeaders = custom_headers;