0

I am trying to send data to an api as x-www-form-urlencoded using Refit. But for some reason the response is not being retrieved. I have followed many tutorials step by step by nothing worked. What might be the problem? Thanks in advance.

Request:

public class SynchronizerApiRequest
    {
        public SynchronizerApiRequest() { }

        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("password")]
        public string Password { get; set; }

        [JsonProperty("last_sync_date")]
        public string LastSyncDate { get; set; }
    }

Response:

public class SynchronizerApiResponse
{
    public SynchronizerApiResponse() { }

    [JsonProperty("success")]
    public bool Success { get; set; }
    
    [JsonProperty("tables")]
    public SyncData Tables { get; set; }
}

SyncData:

    public class SyncData
{
    [JsonProperty("hometab")]
    public List<HomeTab> HomeTabs { get; set; }

    [JsonProperty("category")]
    public List<Category> Categories { get; set; }

    [JsonProperty("doaa")]
    public List<Doaa> Doaas { get; set; }

    [JsonProperty("hijri")]
    public List<Hijri> Hijris { get; set; }

    [JsonProperty("hijri_events")]
    public List<Hijri> HijriEvents { get; set; }
}

Interface:

public interface ISynchronizer
    {
        [Post("/action.php?fn=sync")]
        Task<SynchronizerApiResponse> Request([Body(BodySerializationMethod.UrlEncoded)] SynchronizerApiRequest request);
    }

and finally the code where I am calling the api:

public async Task<SynchronizerApiResponse> GetAllData()
    {
        SynchronizerApiRequest request = new SynchronizerApiRequest()
        {
            Username = "***",
            Password = "***",
            LastSyncDate = Methods.IsEmpty(Methods.GetAppProperty(Constants.LAST_SYNC_DATE)) ? ""
                                  : Methods.GetAppProperty(Constants.LAST_SYNC_DATE)
        };

        syncrhonizer = RestService.For<ISynchronizer>(Constants.BASE_ADDRESS);

        SynchronizerApiResponse response = await syncrhonizer.Request(request);

        System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>>>>>>>>>>." + response.Success + " " + response.Tables.HomeTabs.Count);
        return response;

    }
Sam
  • 291
  • 3
  • 17
  • "response is not being retrieved" - what does this mean? Are you getting any response? What is the HTTP return code? Or does it timeout? Can you get it to work in Postman? – Jason May 26 '21 at 00:26
  • @Jason it works on postman. usually it returns success and an object. in xamarin success is being false anf object is being null – Sam May 26 '21 at 05:18
  • if it works in Postman, you can use Postman to generate the appropriate C# to make the same call. – Jason May 26 '21 at 11:24
  • @Jason i solved it. thank u for ur time – Sam May 26 '21 at 21:07

1 Answers1

0

so I used the newtonsoft json and it worked. the solution is to know how to send your parameters i.e. in which form. below is my solution:

string parameters = "username=X&password=Y&last_sync_date=2020-01-01";

// serialize object to json
var content = new StringContent(parameters, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await Client.PostAsync(Constants.GET_ALL_DATA_URI, content);
string clientresult = response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode();

var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SynchronizerApiResponse>(json);
Sam
  • 291
  • 3
  • 17