B4J Question Curl --cookie

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I am trying to download data from Myqtthub.com site. The data is stored in the MyQttHub cloud service and is downloaded in a csv format.

With help from DonManfred I have been able to login to the site and get the token to continue with the download.

The problem I am having is that the data request is preceded by the token received from the initial login and I am not sure how to format the request with the --cookie request.

Download request:
curl --cookie ‘tokenId=e734b134-0f05-4405-935c-7a4834c0603c’ -X POST https://node02.myqtthub.com/stashed/download 3 -d ‘{“tokenId” : “e734b134-0f05-4405-935c-7a4834c0603c”, “domainName” : “your-hub-domain”, “stashName” : “your-stash-name”, “downloadAs” : “csv”, “downloadFrame” : “all”}’

Error recieved:
ResponseError. Reason: Bad Request, Response: ReactiveMyQttWebInterface.HomeController: Unable to start request for service: /stashed/download, no cookie configured (header tokenId). You'll will have to logout and login

Note: The token is a global string and the value is correct.

Data Download:
Sub get_data
    Dim stringtosend As String = $"{"tokenid" : ${token}, "domainName" : "mydomainName ", "stashName" : "myStash", "downloadAs" : "csv", "downloadFrame" : "all" }"$
    Log(stringtosend)
    Dim j As HttpJob
    j.Initialize("", Me)
    Log("Initialising")
    Dim link As String = "https://node02.myqtthub.com/stashed/download"

    j.poststring(link,stringtosend )
    
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success=True Then
        Log(j.getstring)
        Log("success")
        j.Release
    Else
        'do something'
        Log("didn't work")
        j.Release
    End If

End Sub

Any help gratefully accepted.
Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: the spoiler button only makes it more difficult to read the post.

1. Building json strings like this is a mistake.
2. You will need to use j.GetRequest.SetHeader to set the cookie.

B4X:
j.PostString(...)
 j.GetRequest.SetHeader("Cookie", "tokenId=e734b134-0f05-4405-935c-7a4834c0603c;")
You might also need to set the ContentType.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks Erel,
Still get the same error

The code below firstly gets the token and this works.

B4X:
Sub get_token   
    Dim stringtosend As String = $"{"clientId" : "gmyClientID", "userName" : "myUserName", "password" : "aPassword", "cleanSession" : false }"$
    Dim j As HttpJob
    j.Initialize("", Me)
    Log("Initialising")
    Dim link As String = "https://node02.myqtthub.com/login"
    j.poststring(link,stringtosend )
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success=True Then
        Log(j.getstring)
        Log("success")
        Dim result As String = j.GetString
        Dim s As String = Regex.Split(",",result)(4)
        Log(s)
        Dim t As String = Regex.Split(":",s)(1)
        Log(t)
        Dim l As Int = t.Length
        Dim token As String = t.SubString2(0,l-1)
        Log(token)
        j.Release
    Else
        'do something'
        Log("didn't work")
        j.Release
    End If
    
End Sub

Tried your suggestion

B4X:
Sub get_data
'
    Dim stringtosend As String = $"{"tokenid" : ${token}, "domainName" : "mydomainName ", "stashName" : "myStash", "downloadAs" : "csv", "downloadFrame" : "all" }"$
    Log(stringtosend)
    Dim j As HttpJob
    j.Initialize("", Me)
    Log("Initialising")
    Dim link As String = "https://node02.myqtthub.com/stashed/download"
    Log(token)
    Dim c As String = $"{"tokenId"= ${token};}"$
    Log(c)
    
    j.poststring(link,stringtosend )
    j.GetRequest.SetHeader("cookie",c)
    
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Success=True Then
        Log(j.getstring)
        Log("success")
        j.Release
    Else
        'do something'
        Log("didn't work")
        j.Release
    End If
End Sub

Not sure how to solve this.

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks Erel,

I tried to format the cookie as you suggested in your earlier post with no success.

Am I missing something?
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
B4X:
Dim stringtosend As String = $"{"tokenid" : ${token}, "domainName" : "mydomainName ", ...
add quotation mark to your ${token} variable:
B4X:
Dim stringtosend As String = $"{"tokenid" : "${token}", "domainName" : "mydomainName ", ...
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks for the reply.

This causes a different error

ResponseError. Reason: Bad Request, Response: <!DOCTYPE html>
<html lang="en">
<head>
<title>Bad Request</title>
<style>
html, body, pre {
margin: 0;
padding: 0;
font-family: Monaco, 'Lucida Console', monospace;
background: #ECECEC;
}
h1 {
margin: 0;
background: #AD632A;
padding: 20px 45px;
color: #fff;
text-shadow: 1px 1px 1px rgba(0,0,0,.3);
border-bottom: 1px solid #9F5805;
font-size: 28px;
}
p#detail {
margin: 0;
padding: 15px 45px;
background: #F6A960;
border-top: 4px solid #D29052;
color: #733512;
text-shadow: 1px 1px 1px rgba(255,255,255,.3);
font-size: 14px;
border-bottom: 1px solid #BA7F5B;
}
</style>
</head>
<body>
<h1>Bad Request</h1>
<p id="detail">
For request 'POST /stashed/download' [Invalid Json: Unexpected character (&#x27;f&#x27; (code 102)): was expecting comma to separate Object entries
at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@b7acf1f; line: 1, column: 17]]
</p>
</body>
</html>
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Do this to narrow down the search for the problem:

1. Get hold of a correct cookie value.
2. Hardcode cookie on line 14 to test that everything works as expected, so it looks something like this:

B4X:
j.GetRequest.SetHeader("cookie","tokenId=e734b134-0f05-4405-935c-7a4834c0603c;")

If that works, then the problem is related to how you build the cookie string. And for that, you could read about Smart strings in this thread: https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/

If you are certain that the cookie is correct and it didn't work with you hardcoding it, the problem is elsewhere.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thank you for your reply.

I may be approaching this all wrong.

The instructions for the Myqtthub data retrieval is located at -: https://support.asplhosting.com/t/rest-api-to-manage-myqtthub-service/86

I am using step 1 to login and get the token, then step 2 to process the token, then step 5 to get the csv download.

The token changes ever time you run the program to login and get the token for the next step so I am not sure how to "hardcode" the cookie.

I am thinking this may need to be processed sequentially in one sub routine?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…