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
struct 的文档评论说:
// 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 参考和代码测试没有requestHeaders
使用示例。
我究竟如何设置自己的请求标头字段?
它没有分配任何自定义标头的原因真的很愚蠢 - 它在当前版本中还没有有效且经过测试的实现:
emscripten/ 1.37.13 /system/lib/fetch/emscripten_fetch.cpp :第 78 行
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句