0

So I am trying to call a api in my C# method and am not getting the response that I would be getting, if I called the api using postman or insomia.

public static async  Task GetObject()
        {
            var httpClient = new HttpClient();
            //var t = await httpClient.GetStringAsync("https://ipapi.co/json/");

            var y = await httpClient.GetAsync("https://ipapi.co/json/");


            var o = await y.Content.ReadAsStringAsync();

            var j = y.Content.ReadAsStringAsync();

        }

The api is getting my request but its not returning the right result. You should be getting this result. Click on this https://ipapi.co/json/

What am getting is this

{  
   "error":true,
   "reason":"RateLimited",
   "message":"Sign up for IP Address Location API @ https://ipapi.co"
}

But I don't get it, when am using postman

8
  • And what response are you actually getting? Commented Jun 21, 2019 at 15:24
  • Maybe it's because you are reading the response twice? Either read as json or convert the string to json Commented Jun 21, 2019 at 15:24
  • 1
    Sidenote: your method name and variables are not very clear at representing their meaning. The method GetObject doesn't actually return any data, and if it did, "Object" doesn't tell you what sort of data it is. y, o, and j don't convey any information about the data they hold. Commented Jun 21, 2019 at 15:26
  • @Amy I updated the question Commented Jun 21, 2019 at 15:32
  • @gunr2171 I am first seeing if I can get any type of result that is correct, than I would parse the json into an object. Commented Jun 21, 2019 at 15:33

2 Answers 2

3

You need to add the user-agent header.

httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain how this fixed it?
They are apparently rate-limiting non-browser requests. You would have to ask them.
Some APIs don't allow requests without this header. If you want more detailed explanation you need the ask the developers. When something works in the browser (or postman) but not with code this is the first thing I try.
Thanks @Amy too, won't let me thank both of you in one comment
0

Better to Use xNet.dll or Leaf.xNet.dll libraries Or restsharp one Google them I'm also new If ur interesting to contact me Discord Cyx.#4260

Comments

Your Answer

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.