How do I serialize my class to send it as urlencoded with refit

Hi there I'm trying to send a POST method with refit, using postman so far I can say it's working, if I send the data with the x-www-form-encoded option, the json I send looks like this
{
"apt":"APT",
"apartment":"A103",
"author":"Someone",
"is_public":"True",
"is_complaint":"True",
"show_name":"True",
"title":"fhj",
"details":"vvkko"
}

I constructed my class in visual studio and my model to match it pasting that to json

    namespace App.Models
    {
        public class ManyComplaints
        {
            public SingleComplaint data { get; set; }
        }
        public class SingleComplaint
        {
            public string apt { get; set; }
            public string apartment { get; set; }
            public string author { get; set; }
            public string is_public { get; set; }
            public string is_complaint { get; set; }
            public string show_name { get; set; }
            public string title { get; set; }
            public string details { get; set; }
        }

    }

Here I'm not sure if I did right it's my api caller
[Headers("Content-Type: application/x-www-form-urlencoded")]
[Post("/api/complaints/")]
Task SubmitComplaint([Body(BodySerializationMethod.UrlEncoded)]SingleComplaint complaint);

And in this is the code that's sending the data

    public async Task Post()
        {
            SingleComplaint data = new SingleComplaint {
    is_public = ShowPost,
    is_complaint = Complaint,
    show_name = ShowName,
    author = Preferences.Get("UserName", null),
    apt0 = Preferences.Get("Apt", null),
    apartment = Preferences.Get("Apartment", null),
    title = TitleEntry.Text,
    details = DetailsEntry.Text};

        try
        {                
            var myApi = RestService.For<IApiService>(Constants.webserver);
            var serialized = JsonConvert.SerializeObject(data);
            ManyComplaints complaint = await myApi.SubmitComplaint(data);
            await DisplayAlert("Thanks", "Your message has been succesfully delivered", "Ok");
        }
        catch (Exception ex)
        {
            await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
        }
    }

Tried to use the line as "string complaint = await myApi.SubmitComplaint(serialized);" and also change that as string instead of the "ManyComplaints" class,
also tried to change the model as just the singlecomplaints but I couldn't get it to work, what I'm I missing or how do I make it work?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Can you not serialize this Json data?
    If so, first of all, did you deserializeObject successfully? Or Use this tool to check the model if it is correct.

    Did you add a break point to var serialized = JsonConvert.SerializeObject(data); this line? Check the value of serialized, what is exception that you catched?

  • Yes I did and copied that value to the json validator last time I checked the text view was filled with \ and when turning to json view apparently is missing the curly braces what is wrong :/ do I need to add them separately to the string?
  • This is what I see, postman for example I' mending this as key: json and value: the json in the question
  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited September 17
    var myApi = RestService.For<IApiService>(Constants.webserver);
    var serialized = JsonConvert.SerializeObject(data);
    ManyComplaints complaint = await myApi.SubmitComplaint(data);
    

    Looks to me like you're passing the wrong variable to your SubmitComplaint method.

  • @JoeManke said:

    var myApi = RestService.For<IApiService>(Constants.webserver);
    var serialized = JsonConvert.SerializeObject(data);
    ManyComplaints complaint = await myApi.SubmitComplaint(data);
    

    Looks to me like you're passing the wrong variable to your SubmitComplaint method.

    I also believe that's the problem otherwise I would be getting a 500 error but how should I send it then, the refit code in Iapiservice looks good to me or at least a same one I use for login does work but this one doesn't, the main difference is the login works as raw and this post only as urlencoded

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited September 17

    Is the IApiService interface (and implementation) something you wrote or got from a library?

  • jointothedarksidejointothedarkside Member ✭✭
    edited September 17

    @JoeManke I got it reading their github repository can't really say if it's right or wrong :(
    https://github.com/reactiveui/refit

    [Headers("Content-Type: application/x-www-form-urlencoded")]
    [Post("/api/complaints/")]
    Task SubmitComplaint([Body(BodySerializationMethod.UrlEncoded)]SingleComplaint complaint);

    Also got pointers from this tutorial

  • @JoeManke here's some more information to my understanding refit serialize the data by itself just passing my model, still doesn't work :(
    https://xamgirl.com/consuming-a-restful-web-service-in-xamarin-forms-using-refit-part-1/

Sign In or Register to comment.