• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 101
  • Last Modified:

API not working

Hi all, I had this API working a while ago, however no idea what I have done to make it now not work

			Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
			LocAPI="http://ip-api.com/json/"&Request.ServerVariables("REMOTE_ADDR")&"?fields=countryCode,city"
			oXMLHTTPLoc.Open "GET",LocAPI,False
			oXMLHTTPLoc.Send
			If oXMLHTTPLoc.Status=200 Then
				GetTextFromUrlLoc=oXMLHTTPLoc.responseText
				If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CityLoc=Split(GetTextFromUrlLoc,";")(2)
					Response.Write(CityLoc)
				End If
				If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CountryLoc=Split(GetTextFromUrlLoc,";")(4)
					Response.Write(CountryLoc)
				End If
			End If

Open in new window


The Response.Writes give nothing :(
0
Graeme
Asked:
Graeme
  • 19
  • 14
1 Solution
 
Ryan ChongCommented:
Firstly, i would use a test tool to test and make sure the targeted web API is working file.

to do this, I would recommend to use Postman.
https://www.getpostman.com/

you can download it as a standalone program. Use it to compose a POST/GET request, and send to that targeted web API.

see if the targeted web API returns the expected result?
0
 
Graemewebber4technologiesAuthor Commented:
I have cut and pasted the URL into the browser and its showing its working

http://ip-api.com/json/203.161.69.136?fields=countryCode,city

{"city":"Perth","countryCode":"AU"}

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Checked with Postman, API works :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ryan ChongCommented:
Yup, tested the web API, it's working fine. it returns:

{"city":"Perth","countryCode":"AU"}

Open in new window


what will be the issue is at line:

If ucase(left(GetTextFromUrlLoc,2))="OK" Then

Open in new window

if you do:
response.write "<BR>value = " & ucase(left(GetTextFromUrlLoc,2))

Open in new window

you will get:
{"

Open in new window

which is not equals to "OK"
0
 
Graemewebber4technologiesAuthor Commented:
oh! so how do I fix this?

You helped me with the original code, but yes I some how screwed it up :/
0
 
Ryan ChongCommented:
try this quick fix:
'If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
					Response.Write("<BR>CityLoc = " & CityLoc)
				'End If
				'If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
					Response.Write("<BR>CountryLoc = " & CountryLoc)
				'End If

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Sorry for the lateness in reply, had a stag do last night

Microsoft VBScript runtime error '800a0009'

Subscript out of range: 'Split(...)'

/home.asp, line 61 --> CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")

Open in new window

0
 
Ryan ChongCommented:
this sample works for me. are you sending a valid request to web API?

<%

			Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
			LocAPI="http://ip-api.com/json/"&Request.ServerVariables("REMOTE_ADDR")&"?fields=countryCode,city"
			
			LocAPI="http://ip-api.com/json/203.161.69.136?fields=countryCode,city"
			
			oXMLHTTPLoc.Open "GET",LocAPI,False
			oXMLHTTPLoc.Send
			
			response.write "<BR>oXMLHTTPLoc.Status = " & oXMLHTTPLoc.Status
			
			If oXMLHTTPLoc.Status="200" Then
				GetTextFromUrlLoc=oXMLHTTPLoc.responseText
				
				response.write "<BR>GetTextFromUrlLoc = " & GetTextFromUrlLoc
				
				'If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
					Response.Write("<BR>CityLoc = " & CityLoc)
				'End If
				'If ucase(left(GetTextFromUrlLoc,2))="OK" Then
					CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
					Response.Write("<BR>CountryLoc = " & CountryLoc)
				'End If
			End If

%>

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Using  your code, I get this error:

oXMLHTTPLoc.Status = 200
GetTextFromUrlLoc = {}
Microsoft VBScript runtime error '800a0009'

Subscript out of range: '[number: 1]'

/home.asp, line 70 --> CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")

Open in new window

0
 
Ryan ChongCommented:
GetTextFromUrlLoc = {}
? can you tell what's the content of GetTextFromUrlLoc?
response.write "<BR>GetTextFromUrlLoc = " & GetTextFromUrlLoc
response.end

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Sorry I dont understand
0
 
Ryan ChongCommented:
can you tell us what's the response content after you calling the API?

oXMLHTTPLoc.responseText

response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
response.end

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
I put the line suggested directly below the call of API line
LocAPI="http://ip-api.com/json/"&Request.ServerVariables("REMOTE_ADDR")&"?fields=countryCode,city"
response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
response.end

Open in new window

Error:
msxml3.dll error '80004005'

Unspecified error

/home.asp, line 56 --->response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
sorry wrong area:

Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
LocAPI="http://ip-api.com/json/"&Request.ServerVariables("REMOTE_ADDR")&"?fields=countryCode,city"
oXMLHTTPLoc.Open "GET",LocAPI,False
oXMLHTTPLoc.Send
response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
response.end

Open in new window


response:
oXMLHTTPLoc.responseText = {}

Open in new window

0
 
Ryan ChongCommented:
Request.ServerVariables("REMOTE_ADDR") will return local IP address if it's executed in local environment.

you need to upload your asp file into your web server, which can be accessed publicly in internet and retest. it should give you a public IP address instead and hence your web API will return the correct City and Country accordingly.
0
 
Ryan ChongCommented:
for example, if I run locally, it will get something like:

LocAPI = http://ip-api.com/json/192.168.0.106?fields=countryCode,city

the 192.168.0.106 is not recognizable by the web API and hence no city and country will be returned.
0
 
Graemewebber4technologiesAuthor Commented:
I am testing this from the server, so it is bringing back its correct address: 203 .161.69.136
0
 
Graemewebber4technologiesAuthor Commented:
just tested it, its being run off the server, however it is giving me a 192.168.0.x address...

strange
0
 
Graemewebber4technologiesAuthor Commented:
OK i have fixed it and it is defaulting to external IP

still not getting any results for CityLoc, CountryLoc still

Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")
	If Request.ServerVariables("REMOTE_ADDR")="192.168.0.30" Then
		IP="203.161.69.136"
	Else
		IP=Request.ServerVariables("REMOTE_ADDR")
	End If
	LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	oXMLHTTPLoc.Open "GET",LocAPI,False
	oXMLHTTPLoc.Send
	response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
	If oXMLHTTPLoc.Status="200" Then
		GetTextFromUrlLoc=oXMLHTTPLoc.responseText
		If ucase(left(GetTextFromUrlLoc,2))="OK" Then
			CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
			Response.Write(CityLoc)
		End If
		If ucase(left(GetTextFromUrlLoc,2))="OK" Then
			CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
			Response.Write(CountryLoc)
		End If
	End If

Open in new window

0
 
Ryan ChongCommented:
>>OK i have fixed it and it is defaulting to external IP
are you executing it in your local environment or at your server?

I tried this (to call another external web service to get public IP) to resolve your issue.

<%
    Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")

    LocAPI = "https://api.ipify.org/"
    oXMLHTTPLoc.Open "GET",LocAPI,False
    oXMLHTTPLoc.Send
    If oXMLHTTPLoc.Status="200" Then
        IP = oXMLHTTPLoc.responseText
        Response.write("<BR>IP = " & IP)

        LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	    oXMLHTTPLoc.Open "GET",LocAPI,False
	    oXMLHTTPLoc.Send
	    response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
	    If oXMLHTTPLoc.Status="200" Then
		    GetTextFromUrlLoc=oXMLHTTPLoc.responseText
		    If ucase(left(GetTextFromUrlLoc,2))="OK" Then
			    CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
			    Response.Write(CityLoc)
		    End If
		    If ucase(left(GetTextFromUrlLoc,2))="OK" Then
			    CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
			    Response.Write(CountryLoc)
		    End If
	    End If

    End if
 %>

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
the server is in another room of my house, the routing that have on my LAN gets the DNS entry, but at a local IP. I have tested external with some other friends, it works fine

the IF statement fix works.

your code there has 2x LocAPI, wouldnt that conflict ?
0
 
Ryan ChongCommented:
>>your code there has 2x LocAPI, wouldnt that conflict ?
they will not. Anyway, if you worry about it you can always use another variable for 2nd request.

cheers
0
 
Graemewebber4technologiesAuthor Commented:
Ah not a prob

OK, tried your code:
msxml3.dll error '80090331'

The client and server cannot communicate, because they do not possess a common algorithm.

/home.asp, line 57 --> oXMLHTTPLoc.Send

Open in new window

0
 
Ryan ChongCommented:
>>I have tested external with some other friends, it works fine
yes, that will work fine.

>>the IF statement fix works.
that will make the IP fixed, if I'm in Singapore and got a local ip of 192.168.0.30, it will actually returned the city of mine as Perth but actually i'm in Singapore.
0
 
Graemewebber4technologiesAuthor Commented:
But wouldn't your IP address be your external IP ?

if that was the case, woudlnt almost everyone on a laptop or desktop on a LAN have have the same issue, and getting the same errors..?
0
 
Ryan ChongCommented:
msxml3.dll error '80090331'

The client and server cannot communicate, because they do not possess a common algorithm.

/home.asp, line 57 --> oXMLHTTPLoc.Send

I'm not too sure why you got such error... what if you just try the scripts in comment: ID: 41804407, named it as test.asp, will that work fine?

codes are tested working fine before I posted here.
0
 
Graemewebber4technologiesAuthor Commented:
41804407 - is the exact code I have on my page

nothing changed
0
 
Ryan ChongCommented:
msxml3.dll error '80090331'

The client and server cannot communicate, because they do not possess a common algorithm.

/home.asp, line 57 --> oXMLHTTPLoc.Send
what if you change the code:
LocAPI = "https://api.ipify.org/"

Open in new window

to:
LocAPI = "http://api.ipify.org/"

Open in new window

?
0
 
Graemewebber4technologiesAuthor Commented:
YES! we are getting somewhere! :)

IP = 203.161.69.136
oXMLHTTPLoc.responseText = {"city":"Perth","countryCode":"AU"}

Open in new window


Now just need the shortcuts to produce values :)
0
 
Graemewebber4technologiesAuthor Commented:
Hi Ryan, just wondering how you might be going with this :)
0
 
Ryan ChongCommented:
hi, do you mean to get values into CityLoc and CountryLoc respectively? if yes then try this:
<%
    Set oXMLHTTPLoc=CreateObject("MSXML2.ServerXMLHTTP")

    LocAPI = "http://api.ipify.org/"
    oXMLHTTPLoc.Open "GET",LocAPI,False
    oXMLHTTPLoc.Send
    If oXMLHTTPLoc.Status="200" Then
        IP = oXMLHTTPLoc.responseText
        Response.write("<BR>IP = " & IP)

        LocAPI="http://ip-api.com/json/"&IP&"?fields=countryCode,city"
	    oXMLHTTPLoc.Open "GET",LocAPI,False
	    oXMLHTTPLoc.Send
	    response.write "<BR>oXMLHTTPLoc.responseText = " & oXMLHTTPLoc.responseText
	    If oXMLHTTPLoc.Status="200" Then
		    GetTextFromUrlLoc=oXMLHTTPLoc.responseText
		    CityLoc=Replace(Split(Split(GetTextFromUrlLoc,",")(0),":")(1),"""","")
			Response.Write("<BR>CityLoc = " & CityLoc)
		    
            CountryLoc=Replace(Replace(Split(Split(GetTextFromUrlLoc,",")(1),":")(1),"""",""),"}","")
			Response.Write("<BR>CountryLoc = " & CountryLoc)
	    End If

    End if
 %>

Open in new window

0
 
Graemewebber4technologiesAuthor Commented:
Works Perfect! Thanks!
0
 
Graemewebber4technologiesAuthor Commented:
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 19
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now