Solved

API not working

Posted on 2016-09-16
33
47 Views
Last Modified: 2016-10-16
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
Comment
Question by:Graeme
  • 19
  • 14
33 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802555
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
 

Author Comment

by:Graeme
ID: 41802596
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
 

Author Comment

by:Graeme
ID: 41802598
Checked with Postman, API works :)
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802612
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
 

Author Comment

by:Graeme
ID: 41802615
oh! so how do I fix this?

You helped me with the original code, but yes I some how screwed it up :/
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802616
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
 

Author Comment

by:Graeme
ID: 41803756
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41803758
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
 

Author Comment

by:Graeme
ID: 41803759
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
 
LVL 49

Expert Comment

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

Open in new window

0
 

Author Comment

by:Graeme
ID: 41804311
Sorry I dont understand
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804313
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
 

Author Comment

by:Graeme
ID: 41804314
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
 

Author Comment

by:Graeme
ID: 41804315
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804328
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804329
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Graeme
ID: 41804335
I am testing this from the server, so it is bringing back its correct address: 203 .161.69.136
0
 

Author Comment

by:Graeme
ID: 41804342
just tested it, its being run off the server, however it is giving me a 192.168.0.x address...

strange
0
 

Author Comment

by:Graeme
ID: 41804401
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804407
>>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
 

Author Comment

by:Graeme
ID: 41804410
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804423
>>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
 

Author Comment

by:Graeme
ID: 41804424
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804427
>>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
 

Author Comment

by:Graeme
ID: 41804432
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804433
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
 

Author Comment

by:Graeme
ID: 41804435
41804407 - is the exact code I have on my page

nothing changed
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41804439
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
 

Author Comment

by:Graeme
ID: 41804441
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
 

Author Comment

by:Graeme
ID: 41809004
Hi Ryan, just wondering how you might be going with this :)
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41809982
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
 

Author Closing Comment

by:Graeme
ID: 41810034
Works Perfect! Thanks!
0
 

Author Comment

by:Graeme
ID: 41846169
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now