-1

I'm saving the value of a token in the class variable _Token The token is added to the request header in CreateHttpPostRequest

I make a HTTP request call in PostToJira using CreateHttpPostRequest, and bind the response to OnResponseReceived.

Once i get the response, I call AddScreenshot, where I again make a request using CreateHttpPostRequest, and add a screenshot to the issue. (You can only add a screenshot to an existing issue, which is why we have to make it first, then add the screenshot in a subsequent call.)

The problem is the value of _Token is empty when AddScreenshot is called, and I don't know why.

When debugging the watch value shows this: c++ Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context. error: use of undeclared identifier '_Token'

I've tried using BindLambda instead, thinking that maybe Raw was causing the problem, but it looks like there's something about binding the function that's emptying that member variable. Even when I defined the value of _Token in the header file in lieu of using AuthMan::GetToken(_Token). The value is still empty.

I also tried this->_Token with the same result.

Here are my header and cpp files, sanitized to just show the problem and context:

HtttpUtil.h

class HttpUtil
{
public:
    bool PostToJira();
    
private:    
    void AddScreenshot() const;
    
    void OnResponseReceived(FHttpRequestPtr request, FHttpResponsePtr response, bool bConnectedSuccessfully) const;
    
    TSharedRef<IHttpRequest, ESPMode::ThreadSafe> CreateHttpPostRequest() const;
    
    FString _Token = "my_token";
};

HttpUtil.cpp

bool HttpUtil::PostToJira()
{    
    const FHttpRequestRef request = CreateHttpPostRequest();
    request->OnProcessRequestComplete().BindRaw(this, &HttpUtil::OnResponseReceived);
    request->ProcessRequest();
    return true;
}

void HttpUtil::AddScreenshot() const
{
    const FHttpRequestRef request = CreateHttpPostRequest();
    request->ProcessRequest();
}

void HttpUtil::OnResponseReceived(FHttpRequestPtr request, FHttpResponsePtr response, bool bConnectedSuccessfully) const
{
    AddScreenshot();
}

TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpUtil::CreateHttpPostRequest() const
{
    TSharedRef<IHttpRequest, ESPMode::ThreadSafe> request = FHttpModule::Get().CreateRequest();
    request->SetURL("http://echo.jsontest.com/key/value/one/two");

    // When this function is called by AddScreenshot(), the _Token value is empty
    UE_LOG(LogConsoleResponse, Warning, TEXT("_Token = %s"), *_Token);
    return request;
}
  • 1
    This doesn’t address the question, but names that begin with an underscore followed by a capital letter (`_Token`) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code. – Pete Becker Jun 16 '23 at 01:02
  • 1
    I'm guessing your class has been destructed by the time your callback executes. Please show a [mre] – Alan Birtles Jun 16 '23 at 06:05
  • @Alan Birtles, updated the code – MashedPotato6587 Jun 16 '23 at 14:54
  • After testing, I confirmed Alan's guess that the class is being destructed before the callback is happening. Is there a way to keep the class alive until the callback happens? – MashedPotato6587 Jun 16 '23 at 15:03

1 Answers1

0

The problem I was running into was that the class was being destructed before the callback was happening. My solution was to not use member variables.