My logInAndFetch() function (below for reference), works as it should, returning a valid API Key just as it does when inspecting the portal on my browser.
I am quite confident about the parsing of the variable apiKey before passing it to the fetchData() function, as I have tested it in my browser when inspecting too.
void logInAndFetch ()
{
juce::URL authUrl("OTHER PORTAL");
juce::String postData = "Username=" + getUsername() + "&Password=" + getPassword();
authUrl = authUrl.withPOSTData (postData);
auto inputStream (authUrl.createInputStream (postOptions.withExtraHeaders("Accept: text/json")));
if (inputStream)
{
auto apiKey = inputStream->readEntireStreamAsString();
DBG (apiKey);
apiKey = apiKey.fromFirstOccurrenceOf ("\"apikey\":", false, true).trimCharactersAtEnd ("}");
DBG (apiKey);
fetchData (apiKey);
}
else
{
DBG("Failed to send data to URL");
DBG(authUrl.toString (true));
}
}
My problem happens in my fetchData() function. When I try to GET the data onto my stream variable, it is always empty. I have added a breakpoint as well, indicating stream is simply empty.
void fetchData(juce::String apiKey)
{
juce::URL userInfoUrl ("SOME PORTAL");
auto inputStream (userInfoUrl.createInputStream(getOptions.withExtraHeaders ("Authorization: apikey" + apiKey + "\n" +
"Accept: text/json")
.withConnectionTimeoutMs(10000)));
if (inputStream)
{
juce::String stream = inputStream->readEntireStreamAsString();
DBG (stream);
}
}
Is there a problem with the way I am setting the headers, for example? Any help would be appreciated.
this is the original working Javascript code:
//Get info for current user
$.ajax({
type: "GET",
url: "MY_PORTAL",
headers: {
"Authorization": "apikey " + apikey,
"Accept": "text/json"
},
complete: function(r) { console.log([r.status,r.responseText]); }
});
btw getOptions and postOptions are constants defined such as:
const juce::URL::InputStreamOptions getOptions = juce::URL::InputStreamOptions (juce::URL::ParameterHandling::inAddress);
const juce::URL::InputStreamOptions postOptions = juce::URL::InputStreamOptions (juce::URL::ParameterHandling::inPostData);