UE5 C++自定义Http节点获得Header数据

一、新建C++文件

选择All Classes,选择父类BlueprintFunctionLibrary,命名为SendHttpRequest。

添加Http支持

代理回调的参数使用DECLARE_DYNAMIC_DELEGATE_TwoParam定义,第一参数是代理类型,后面是参数1类型,参数1,参数2类型,参数2。

代理通过UPROPERTY声明

UFUNCTION的BlueprintCallable是定义一个带有流程的节点

复制代码
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "SendHttpRequest.generated.h"
DECLARE_DYNAMIC_DELEGATE_TwoParams(FHttpResponseDelegate,bool,bSuccess,const TArray<FString>&,headers);
UCLASS()
class REDSTORY_API USendHttpRequest : public UBlueprintFunctionLibrary
{
	
	GENERATED_BODY() public:
		UPROPERTY()
		FHttpResponseDelegate HttpResponseDelegate;
	
		UFUNCTION(BlueprintCallable, Category = "Custom", meta = (Keywords = "SendHttpRequest"))
		static void SendHttpRequest(FHttpResponseDelegate Callback);
	
		// void Response(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded);
};

// Fill out your copyright notice in the Description page of Project Settings.
#include "SendHttpRequest.h"
#include "HttpModule.h"
#include "Interfaces/IHttpResponse.h"
void USendHttpRequest::SendHttpRequest(FHttpResponseDelegate Callback)
{
	
	FHttpModule* Http = &FHttpModule::Get();
	TSharedRef<IHttpRequest,ESPMode::ThreadSafe> Request = Http->CreateRequest();
	Request->SetURL("http://127.0.0.1:8888/a");
	Request->SetVerb("POST");
	Request->SetHeader("Content-Type", "application/json");
	Request->SetHeader("User-Agent","X-UnrealEngine-Agent");
	Request->SetContentAsString("{\"a\": 1,\"b\": \"2\",\"c\": \"3\",\"d\": \"4\"}");
	// Request->OnProcessRequestComplete().BindUObject(this,&USendHttpRequest::Response);
	Request->OnProcessRequestComplete().BindLambda([Callback](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
	{
		//UE_LOG(LogTemp,Display,TEXT("Response %s"),*Response->GetHeader("Set-Cookie"));
		Callback.ExecuteIfBound(bWasSuccessful,Response->GetAllHeaders());
	});
	Request->ProcessRequest();
}

// void USendHttpRequest::Response(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)
// {
//
// 	//UE_LOG(LogTemp,Display,TEXT("Response %s"),*HttpResponse->GetContentAsString());
// 	// UE_LOG(LogTemp,Display,TEXT("Response %s"),*HttpResponse->GetHeader("Set-Cookie"));
// 	// HttpResponseDelegate.Broadcast(bSucceeded);
// }

二、Blueprint

使用UE_LOG做测试还挺好用。

问题:no value will be returned by reference

数组作为代理的参数的写法

复制代码
DECLARE_DYNAMIC_DELEGATE_TwoParams(FHttpResponseDelegate,bool,bSuccess,const TArray<FString>&,headers);

问题:Compiler In use pin <Unnamed> no longer exists on node Send Http Request .

删除项目如下文件夹,然后鼠标右键点击uproject文件,重新生成这些文件。

相关推荐
sinat_2554878121 小时前
第七部分。介绍MVC(模型-视图-控制器)模式
java·ide·http·tomcat·intellij-idea
齐鲁大虾1 天前
如何彻底解决从公网HTTP页面请求私有HTTP资源跨域问题
网络·网络协议·http
韩曙亮1 天前
【Flutter】Flutter 中的 Android / iOS 特殊配置 ① ( 网络权限配置 | HTTP 明文传输配置 | 应用名称配置 )
android·网络·flutter·http·ios·网络权限
WIZnet1 天前
W55RP20-EVB-MKR 模块 MicroPython 实战 (11):HTTP 协议与 OneNET 平台数据上云
网络·网络协议·http
许彰午1 天前
微服务安全上下文的透明传递——ThreadLocal透传与HTTP头转发的完整链路
安全·http·微服务
触底反弹1 天前
从 Bun 到 DeepSeek:用 TypeScript 构建你的第一个 AI Agent
人工智能·http·typescript
开开心心就好1 天前
清理重复文件释放C盘空间的工具
安全·智能手机·pdf·gitlab·音视频·intellij idea·1024程序员节
遇事不決洛必達1 天前
【爬虫随笔】深入理解 HTTP/HTTPS 协议、接口交互与会话机制
爬虫·网络协议·http·https·session
华山令狐虫1 天前
DBAPI 接入 Milvus 向量数据库:HTTP 执行器参数映射实战
数据库·http·milvus·dbapi
代码中介商2 天前
HTTP 完全指南(最终篇):CORS 跨域资源共享深度详解
网络·网络协议·http