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);
 }
LarsTech
81.8k14 gold badges161 silver badges235 bronze badges
asked Sep 5, 2016 at 13:39
5
  • What's the HTML, and what's the returned HTTP status code? Commented Sep 5, 2016 at 13:42
  • @stuartd This is the beginning of HTML received '<!DOCTYPE html>' and HTTP status code = 200 Commented Sep 5, 2016 at 13:59
  • @FabrizioMigotto Yes, I have checked that API is right. 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? 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.

answered Sep 5, 2016 at 14:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.