Question

I am attempting to add a row to a Class in my Parse database using the API

I have managed to get the row added but I noticed that both the 'ACL & user__user' fields are blank.

My Request class looks like this:

public class AccountDataRequest
    {
        public string OrderNo { get; set; }
        public string SiteName { get; set; }
        public string CreditDebit { get; set; }
        public int Amount { get; set; }
        public int CreditBalance { get; set; }
    }

And my function looks like this:

public static AccountDataResponse AddTransaction(AccountDataRequest details)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.parse.com/1/classes/AccountData");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add("X-Parse-Application-Id", "xxxxx");
        httpWebRequest.Headers.Add("X-Parse-REST-API-Key", "xxxxx");

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = JsonConvert.SerializeObject(details);

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                return JsonConvert.DeserializeObject<AccountDataResponse>(result);
            }
        }
    }

The 'user__user' links to the 'objectID(String)' in mu Users class.

I have tried adding it to my class as a string but it throws and error.

Can anyone tell me how to get the ACL and 'user__user' populated please?

Thanks

UPDATE:

I have learnt that the Pointer is a structure so have amended my classes as follows:

public class AccountDataRequest
{
    public string OrderNo { get; set; }
    public string SiteName { get; set; }
    public string CreditDebit { get; set; }
    public double Amount { get; set; }
    public int CreditBalance { get; set; }
    public Pointer Pointer { get; set; }
}

public class Pointer
{
    public string __type { get; set; }
    public string className { get; set; }
    public string objectId { get; set; }
}

So, when I now call the API with the following:

POST https://api.parse.com/1/classes/AccountData HTTP/1.1
Content-Type: application/json
X-Parse-Application-Id: xxxxx
X-Parse-REST-API-Key: xxxxx
Host: api.parse.com
Content-Length: 163
Expect: 100-continue
Connection: Keep-Alive

{"OrderNo":"9","SiteName":"Trev","CreditDebit":"Debit","Amount":1.0,"CreditBalance":999,"Pointer":{"__type":"Pointer","className":"AccountData","objectId":"fvJ8jPjyjx"}}

This call produces no errors and returns a createddate and an objectid but the user(Pointer <__User> is still null.

I also tried changing the className to "__User" to which the API responds:

{"code":111,"error":"invalid type for key Pointer, expected *AccountData, but got *_User"}

This is what the empty column looks like in my AccountData class

enter image description here

And I am trying to tie it to the objectId in my User table:

enter image description here

Can anyone see what is wrong please?

Was it helpful?

Solution

If the column in Parse is defined as a Pointer to _User, then you need to pass the appropriate structure to Parse and not just the objectId. Take a look at the Data Types section of the Parse REST SDK. It has a paragraph and sample for passing a Pointer structure through the API.

Similarly, for ACLs you need to specify that structure as well. Details for it are in the security docs for the SDK.

In both cases, if you want to use an object->JSON converter, you'll need to build an object that appropriately represents that structure. That'll be a bit easier for Pointers than ACLs as the former has predefined keys whereas the latter uses dynamic key names (e.g. objectIds and role names).

Updated to include class definition

public class AccountDataRequest
{
    public string OrderNo { get; set; }
    public string SiteName { get; set; }
    public string CreditDebit { get; set; }
    public double Amount { get; set; }
    public int CreditBalance { get; set; }
    public Pointer user { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top