# Didn't Get any Response from Http Request

**URL:** <https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593>\
**Category:** C++\
**Tags:** unreal-engine, UE5-0, CPP, question\
**Created:** [November 11, 2022, 9:23am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593 "2022-11-11T09:23:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickzw](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@patrickzw](https://forums.unrealengine.com/u/patrickzw)\
**Post date:** [November 11, 2022, 9:23am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/1 "2022-11-11T09:23:49Z")

</div>

Hi,

I was trying to write a login system in my game connected to my own server. After searching online, I used the MySQL database and python flask on my server to receive and give the information.

The question is: The server got the request which had the information I entered in the game and should return a JSON message, but the game did not receive it. The response function I bound to the request is never executed in c++.

Here is the part of the code I wrote.

In HTTPService.cpp:

```auto
FHttpRequestRef UHTTPService::RequestWithRoute(FString Subroute)
{
	FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
	FString TargetURL = TEXT("My IP Address") + Subroute;
	Request->SetURL(TargetURL);
	UE_LOG(LogTemp, Warning, TEXT("Request URL is: %s"), *Request->GetURL());
	SetRequestHeader(Request);
	return Request;
}

void UHTTPService::SetRequestHeader(FHttpRequestRef& Request)
{
	Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
	Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
	Request->SetHeader(TEXT("Accepts"), TEXT("application/json"));
}

FHttpRequestRef UHTTPService::GetRequest(FString Subroute)
{
	FHttpRequestRef Request = RequestWithRoute(Subroute);
	Request->SetVerb("GET");
	return Request;
}

FHttpRequestRef UHTTPService::PostRequest(FString Subroute, FString JsonString)
{
	FHttpRequestRef Request = RequestWithRoute(Subroute);
	Request->SetVerb("POST");
	Request->SetContentAsString(JsonString);
	return Request;
}

void UHTTPService::Send(FHttpRequestRef& Request)
{
	Request->ProcessRequest();
}

//This part is executed.
void UHTTPService::StartLogin(FLogin_Request LoginCredentials)
{
	FString JsonString;
	GetJsonStringFromStruct<FLogin_Request>(LoginCredentials, JsonString);
	UE_LOG(LogTemp, Warning, TEXT("JsonString is: %s"), *JsonString);

	TSharedRef<IHttpRequest> Request = PostRequest("login", JsonString);
	Request->OnProcessRequestComplete().BindUObject(this, &UHTTPService::LoginResponse);
	Send(Request); 
}

// This method is never executed
void UHTTPService::LoginResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	if (!ResponseIsValid(Response, bWasSuccessful)) return; 

	FLogin_Response LoginResponse;
	GetStructFromJsonString<FLogin_Response>(Response, LoginResponse);

	UE_LOG(LogTemp, Warning, TEXT("Id is: %d"), LoginResponse.Id);
	UE_LOG(LogTemp, Warning, TEXT("Name is: %s"), *LoginResponse.UserName);
}

bool UHTTPService::ResponseIsValid(FHttpResponsePtr Response, bool bWasSuccessful)
{
	if (!bWasSuccessful || !Response.IsValid()) return false;
	if (EHttpResponseCodes::IsOk(Response->GetResponseCode())) return true;
	else {
		UE_LOG(LogTemp, Warning, TEXT("Http response return error code: %d"), Response->GetResponseCode());
		return false;
	}
}

```

On the server, I wrote these:

login.py:

```auto
@app.route('/login', methods=['GET', 'POST'])
def login():
	content = request.get_json(force=True)

	if request.method == 'POST':
		print ('Incoming request...')
		username = content['username']
		password = content['password']

		print ('The username:', username, ' and password: ', password)
		cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
		cursor.execute('SELECT * FROM accounts WHERE username = %s AND password = %s', (username, password))
		account = cursor.fetchone()

		if account:
			print ('Found user in database...')
			session['loggedin'] = True
			session['username'] = account['username']
			session['id'] = account['id']
			print ('Returning the message...')
			return jsonify({'id':account['id'], 'username':account['username'], 'message':'Logged in successfully.'})
		else:
			print ('Did not find the user in database...')
			print ('Sending back error message...')
			message = {'message':'Incorrect username or password.'}
	print ('Sending back error message...')
	return jsonify(message)

```

The result in the server when running “login.py” is this:  
 ![server](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/a/c1a5b7a1166377fe03418bb6b66b81381733d112.png)

The output log in the game is:  
 ![client](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/d/b/fdbff0274545d887466859336522b23104a9751b.png)  
It stops here, and not executed the “LoginResponse” method.

Hope this information can help you understand what I did and please give me some advice on how to fix the problem. Thanks.

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [November 11, 2022, 9:36am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/2 "2022-11-11T09:36:22Z")

</div>

Hey, not sure if you did it for your own Security, or I don’t have function on my end here, but I don’t see implementation of `PostRequest` method.

Here is the code I used for my game, and it works fine. So check it out, and see if you missed something

```auto
	// Create HTTP Request
	TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
	HttpRequest->SetVerb("GET");
	HttpRequest->SetHeader("Content-Type", "application/json");
	HttpRequest->SetURL("https://myWebSite/MyFile.php");

	// Setup Async response
	HttpRequest->OnProcessRequestComplete().BindUObject(this, &MyClass::ServerListPopulateComplete);
	HttpRequest->ProcessRequest();

```

Also, check these out, maybe you will find some solution

> [@HTML5 HTTPRequest: OnProcessRequestComplete not called](https://forums.unrealengine.com/t/html5-httprequest-onprocessrequestcomplete-not-called/314171):
>
> I have run into some trouble with the HttpRequest-\>OnProcessRequestComplete function when I test my project in HTML5. The PostRequest function works just fine (because the data is logged in the database table) but the RequestComplete function doesn’t seem to fire. Everything works fine in the dekstop builds. (UE 4.7.4 - binary, emscripten 1.29.0) Could this be a bug or am I missing something? PostRequest Functionality void UConnection::PostRequest(UObject\* WorldContextObject) { TSharedRef\<I…

> [@HTTP request don't run on HTML5 package](https://forums.unrealengine.com/t/http-request-dont-run-on-html5-package/318689/8):
>
> Hey !! Excuse me, I didn’t know which console you were referred. Ok: When I execute the game on HTML the 's console shows: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.16:3334/query\_obra.php?id=1. (Reason: CORS header 'Access-Control-Allow-Origin' missing).1 \<unknown\> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.16:3334/query\_obra.php?id=1. (Reason: C…

> [@No response (fail or success) on HTTP calls](https://forums.unrealengine.com/t/no-response-fail-or-success-on-http-calls/489259):
>
> We’ve used HTTP requests to a REST API since UE 4.24 without issues. Recently we’ve started getting problems with requests not completing - neither with fail nor success nor timeout. I’ve used Request-\>OnProcessRequestComplete() to bind to lambda or UObject, which has always triggered the function to be called in some way (whether it’s with success or failure). Now nothing happens, and I can’t see any incoming traffic on the server either. I’ve tried to bind to the progress callback too without …

Try to google out unreal engine `OnProcessRequestComplete not executed`, I found couple of threads.

---

<div class="post-metadata">

**Author:** ![patrickzw](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@patrickzw](https://forums.unrealengine.com/u/patrickzw)\
**Post date:** [November 11, 2022, 9:42am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/3 "2022-11-11T09:42:15Z")

</div>

Hi, thanks for your reply, I forgot to add the post method and now I edit my question. Please take a look. And I will check the post you linked here and google the threads to see if those can fix my problem. Thanks again.

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [November 11, 2022, 9:47am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/4 "2022-11-11T09:47:39Z")

</div>

OK, I see it now. Chek out my code also. Even though it’s “GET”.  
As I said, it works for me.

Also check out this video: [Unreal Engine C++ Persistence #5: "Send POST Request" - YouTube](https://youtu.be/VuIAu14O55k)

Aaand, I googled this example out 🙂

```auto
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetURL("http://localhost/api.php");
Request->SetVerb("POST");

Request->SetContentAsString(JsonString);
Request->OnProcessRequestComplete().BindUObject(this, &UAuthWidget::OnResponseReceived);
Request->ProcessRequest();
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
Request->SetHeader(TEXT("Accepts"), TEXT("application/json"));
Request->SetHeader(TEXT("Accept-Encoding"), TEXT("identity"));

```

---

<div class="post-metadata">

**Author:** ![patrickzw](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@patrickzw](https://forums.unrealengine.com/u/patrickzw)\
**Post date:** [November 11, 2022, 9:49am UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/5 "2022-11-11T09:49:26Z")

</div>

Thank you very much! I will try these out.

---

<div class="post-metadata">

**Author:** ![patrickzw](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@patrickzw](https://forums.unrealengine.com/u/patrickzw)\
**Post date:** [November 11, 2022, 12:45pm UTC](https://forums.unrealengine.com/t/didnt-get-any-response-from-http-request/688593/6 "2022-11-11T12:45:56Z")

</div>

I think I know what is the problem. There is something wrong with the UObject class. I write my UHTTPService.h under UObject class.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/6/e96b154bf1958fe3b026bcf90bb1209152293ed4.png)

After I test your code in the Actor class inherited from AActor and paste mine code into the same Actor class. it works. Everything is fine.  
So I guess that the `Request->OnProcessRequestComplete().BindUObject()` function does not work in the UObject class somehow.

The reason why I used the UObject is that I want to make it portable so that when I need this feature, I can just copy and paste the files into the new projects and include them in other c++ files. I guess I can take it with AActor for now. If this is not the proper way to do such a thing, do you know which class or which way I should do it to make it portable?

Anyway, thanks, after three hours, finally knows where is the problem. Soooooo tired.
