如何从 C++ 为 emscripten Fetch API 设置自定义标头字段

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_tstruct 的文档评论说:

// 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++类中的成功回调Emscripten FETCH API

我们可以在Azure Active Directory B2C自定义策略中的RESTful API调用上设置自定义标头吗?

无法为http目标设置自定义标头字段

NodeJS / Express:如何将自定义标头设置为所有res.render但未设置为res.send?

如何在WordPress中为自定义标头实现引导小部件代码?

如何在无服务器中为自定义标头提供CORS?

如何在angularjs应用程序中为Bootstrap Popover添加自定义标头

如何使用API获取/设置Trello自定义字段?

如何使用 Fetch api 获取 CORS 请求中的标头字段

如何使用phpmailer设置自定义标头

如何使用PJAX设置自定义xhr标头?

如何设置webgrid标头的自定义字体颜色?

如何在Web API消息处理程序中提取自定义标头值?

如何在Swashbuckl ASP.NET Core Web API上添加自定义标头

如何将自定义标头从 mvc 项目发送到 web api 项目?

如何使用JavaScript Fetch API发送授权,Cookie标头

C#使用自定义标头调用Salesforce Rest Api

http响应状态为400时,使用fetch api获取自定义错误消息

HTTP :: Daemon:如何在HTTP标头中为服务器设置自定义值?

如何在 c# WPF 中为自定义形状对象设置动画

如何使用 C# DocuSign 的 API 查看一批信封的自定义字段

如何自定义xml标头

向 Aurelia Fetch 请求添加自定义标头

如何使用WSO2 API Manager在Mediator中将租户设置为标头

如何使用QuickBooks V3 DataService为“发票自定义”字段设置值?

如何使用Spring Boot使用OAuth1.0和自定义标头对API进行身份验证

如何将自定义标头添加到ASP.NET Core Web API响应

wso2 api 管理器如何向传递到后端的请求添加自定义标头

如何将自定义标头参数添加到 clojurescript 中的 API GET 调用