Page 1 of 1

HttpWebRequest to Appery Db

Posted: Fri Aug 14, 2015 3:33 am
by Aeneas McBurney

I'm hoping someone can help me about retrieving data from the Appery database through .net. I have been able to get data without using a query filter as below

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
request = DirectCast(Net.WebRequest.Create("https://api.appery.io/rest/1/db/colle..."), HttpWebRequest)
request.Headers.Add("X-Appery-Database-Id", "54f7de62xxxxxxxxxxxxxx")
request.Headers.Add("X-Appery-Master-Key", "eadcda63-5cfxxxxxxxxxxx")
request.ContentType = "application/json"
request.Method = "GET"

Code: Select all

        ' Get response   
         response = DirectCast(request.GetResponse(), HttpWebResponse) 

My issue is if I try to pass a where clause to the data I get Bad Request ("{"code":"DBSP260","description":"Object must be present in the request body"}") returned. This is my code to try and pass parameters which has to be a POST method...

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
request = DirectCast(Net.WebRequest.Create("https://api.appery.io/rest/1/db/colle..."), HttpWebRequest)
request.Headers.Add("X-Appery-Database-Id", "54f7de62xxxxxxxxxxxxxx")
request.Headers.Add("X-Appery-Master-Key", "eadcda63-5cfxxxxxxxxxxx")
request.ContentType = "application/json"
request.Method = "POST"

Code: Select all

         Dim params As String = """where"":{""_id"":""55af51f1e4b0f840f33b6a49""}" 

Dim requestWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
Dim reqWriter As Stream = request.GetRequestStream
requestWriter.Write(params)
requestWriter.Close()

Code: Select all

        ' Get response   
         response = DirectCast(request.GetResponse(), HttpWebResponse) 

I have been going around in circles so I'm hoping someone can help about the correct way to pass parameters with this request method. I have also tried adding the parameters tag in the where clause

"parameters:{where:{""_id"":""55af51f1e4b0f840f33b6a49""}}"

but get same result of Bad Request ("{"code":"DBSP260","description":"Object must be present in the request body"}")
.

I am able to do it fine in server code in javascript using XHR.send

Thanks!


HttpWebRequest to Appery Db

Posted: Fri Aug 14, 2015 8:20 am
by Bad Addy

Not sure if this will help you, i have not used ASP in a very long time:

http://stackoverflow.com/questions/10...

But that said, for security reasons, I would not use the master key. Would be safer to use server side code, and execute it that way :)


HttpWebRequest to Appery Db

Posted: Fri Aug 14, 2015 8:39 pm
by Aeneas McBurney

Thanks for your response and link but it's not for Json data types which is what this db is. I'm unable to use server code as it timesout through Apperys settings.

Hoping someone else can help.


HttpWebRequest to Appery Db

Posted: Tue Aug 18, 2015 8:34 am
by Alena Prykhodko

Dear Aeneas,

Unfortunately we do not have solution to share, and this is something outside the scope (http://devcenter.appery.io/support-po...) of our support. Hope someone from community will be able to help with this.


HttpWebRequest to Appery Db

Posted: Wed Dec 02, 2015 7:20 pm
by Aeneas McBurney

If anyone is looking to do this I have worked it out and below is a code example of how to call from VB .NET

Dim params As String = "where={_id:'" & userID & "'}"
request = DirectCast(Net.WebRequest.Create("https://api.appery.io/rest/1/db/colle...?" & params), HttpWebRequest)
'request = DirectCast(Net.WebRequest.Create("http://httpbin.org/get?" & params), HttpWebRequest)
request.Headers.Add("X-Appery-Database-Id", "54xxxxxxxxxx")
request.Headers.Add("X-Appery-Master-Key", "eadcdxxa63-5cf3-481fxxxxx")
request.ContentType = "application/json; charset=utf-8" '"application/x-www-form-urlencoded"
request.Method = "GET"

Code: Select all

         Using response = request.GetResponse() 
             Using stream As Stream = response.GetResponseStream() 
                 Dim reader As New StreamReader(stream) 
                 Dim jsonstr As String = "{Data:" & reader.ReadToEnd() & "}" 
                 Dim json As JObject = JObject.Parse(jsonstr) 
                 'Dim jResults = JsonConvert.DeserializeObject(jsonstr) 
                 'Dim results As List(Of JToken) = jResults.Children().ToList() 
                 For Each row In json("Data") 

                     MsgBox(row("_id")) ' because my tag in json is img 

                 Next 

                 MsgBox(jsonstr) 
                 Return jsonstr 

             End Using 
         End Using