Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

HttpWebRequest to Appery Db

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!

Bad Addy
Posts: 0
Joined: Fri Dec 13, 2013 9:34 pm

HttpWebRequest to Appery Db

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 :)

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

HttpWebRequest to Appery Db

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.

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

HttpWebRequest to Appery Db

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.

Aeneas McBurney
Posts: 0
Joined: Mon Jun 16, 2014 7:49 am

HttpWebRequest to Appery Db

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

Return to “Issues”