2

Don't know why a web request return HTML instead JSON. Can anyone please help.

private void Test()
    {
        string url = "https://www.netonnet.no/Category/GetFilteredCategory";

        string json = "{'sectionId':'10978','filter': '[]','sortOrder':-1,'sortBy':0,'pageSize':96,'listType':'10'}";

    string result = "";
        using (var client = new WebClient())
        {                
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            result = client.UploadString(url, "POST", json);
        }
        Debug.WriteLine(result);
    }
5
  • What's the HTML, and what's the returned HTTP status code?
    – stuartd
    Commented Sep 5, 2016 at 13:42
  • @stuartd This is the beginning of HTML received '<!DOCTYPE html>' and HTTP status code = 200
    – AAP
    Commented Sep 5, 2016 at 13:59
  • @FabrizioMigotto Yes, I have checked that API is right.
    – AAP
    Commented Sep 5, 2016 at 14:10
  • I checked with the same request - the content type of entity returned is text/html; charset= utf-8.you cannot expect json here. Commented Sep 5, 2016 at 14:13
  • @AmitKumarGhosh Many thanks, for my knowledge why and which conditions that happens?
    – AAP
    Commented Sep 5, 2016 at 14:15

1 Answer 1

1

When your asking an you want it in a specific format you should add

client.Headers[HttpRequestHeader.Accept] = "application/json";

This will tell the API that you want it in json, but this only works if they can give it to you in that format.

And like Amit Kumar Ghosh said in a comment above, it seems like they don't serve json.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.