简体   繁体   中英

API GET Request Returns HTML/Text instead of JSON

This is my first time working with a RESTful API and Xamarin and etc. I have so far made a simple REST API. I have written a GET call to it that, if I write http://localhost:[num]/api/Name , it will return a JSON file of the matching Emu's information. I have tested this with Postman, so I know that it works.

I have now written an app that will call this API in order to catch this information and then display it. So far, I've got it connected to the server hosting my API, but I'm unable to get it to return JSON. Instead it seems to be returning text/HTTP.

From what I've searched up on previous Stack Overflow threads, it seems that I was missing Headers requesting that reply be in a JSON format. When I added in code that was on the official .NET documentation on Microsoft's website, it gave me issues with my Json Deserialiser. I have also added in the information in the header to make sure that it returns json.

Here is the code for the function:

async private void Submit_OnClicked(object sender, EventArgs e)
        {
            var nameValue = EmuName.Text;

            var baseAddr = new Uri("http://my_url/HelloEmu/");
            var client = new HttpClient { BaseAddress = baseAddr };
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            string url = (string)nameValue;
            var returnedJson = await client.GetStringAsync(url);

            Models.EmuItemModel MyEmu = JsonConvert.DeserializeObject<Models.EmuItemModel>(returnedJson);


            ReturnedName.Text = MyEmu.Name;
            ReturnedAge.Text = MyEmu.Age.ToString();
            ReturnedWeight.Text = MyEmu.Weight.ToString();

My code actually faults on the line ReturnedWeight.Text = MyEmu.Weight.ToString() But I'm guessing the more majour issue is occuring during deserialisng the object, because it seemingly "skips" over the preceeding two lines when I run it in the debugger.

When I run it in Visual Studio 2019, the value of "returnedJson" is this:

"<html><head><meta http-equiv=\"refresh\" content=\"0;url=http://lookup.t-mobile.com/search/?q=http://my_url/HelloEmu/Keith&t=0\"/></head><body><script>window.location=\"http://lookup.t-mobile.com/search/?q=\"+escape(window.location)+\"&r=\"+escape(document.referrer)+\"&t=0\";</script></body></html>" 

I think this is an HTML output. I would love any hints about what on earth I'm doing wrong!

EDIT: Since it almost seems like the HTML is returning an error message, perhaps it could do with my url??? I've published the website using the File system method. So to access the API in Postman I'll use http://localhost:[port]/api/values , calling my website in a regular ol' browser makes it go http://my_url/HelloEmu . I get a 403 "no directory" method in return...

EDIT: Here is the Postman code: enter image description here

Usually it happens because there are missing headers or some other malformed request, Download RestSharp DLL from NuGet, and then you can use the following, in postman, go to "Code":

在此处输入图片说明

And choose C# you will see a code snippet (Example):

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM