function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitin sharma 425Nitin sharma 425 

URL and Json Issue

HI All.
I am making a webservice call out and I am getting the following reponse which I am not able to fix.


The body has the following reponse{"error": "Invalid Parameters supplied."}

If I  direclty type URL with Parameters in the Browser then it gets me data


https://api.weatherbit.io/v2.0/current?city=hudson&key=b3dbd1fd17094d04adab62cb4e9b8d23



{"data":[{"rh":65,"pod":"d","lon":-82.69343,"pres":1008.5,"timezone":"America\/New_York","ob_time":"2021-04-17 21:16","country_code":"US","clouds":75,"ts":1618694160,"solar_rad":425.4,"state_code":"FL","city_name":"Hudson","wind_spd":0.89,"wind_cdir_full":"west-southwest","wind_cdir":"WSW","slp":1009.2,"vis":5,"h_angle":60,"sunset":"23:57","dni":831.43,"dewpt":21.7,"snow":0,"uv":3.24984,"precip":0,"wind_dir":254,"sunrise":"11:01","ghi":608.39,"dhi":103.24,"aqi":52,"lat":28.36445,"weather":{"icon":"c02d","code":802,"description":"Scattered clouds"},"datetime":"2021-04-17:21","temp":28.9,"station":"C9863","elev_angle":37.99,"app_temp":31.7}],"count":1}


However,After constructing the same URL in the code I get the following error 


The body has the following reponse{"error": "Invalid Parameters supplied."}


So basically it looks like an issue with my URL.How do I fix it?


Here the rest link from the service supplier

Base URL.

HTTP: http://api.weatherbit.io/v2.0/current
HTTPS: https://api.weatherbit.io/v2.0/current

Followwing is the Api Key

b3dbd1fd17094d04adab62cb4e9b8d23

and I am passing Hudson as the city.

City=Hudson


Here is my URL which I constructed in the code:-

String k='b3dbd1fd17094d04adab62cb4e9b8d23'; 
            city=EncodingUtil.urlEncode(city,'UTF-8'); 

   
            String MapUrl='https://api.weatherbit.io/v2.0/currentweather?q='+city+'& Key'=+k;
            
However,it gives me an error when I run code.

Can somebody help me in fixing this URL.




 
ShivankurShivankur (Salesforce Developers) 
Hi Nitin,

I have tried running a test with the endpoint shared above in a dev org and following code should work fine for you:
Http http = new Http();

HttpRequest request = new HttpRequest();

request.setEndpoint('https://api.weatherbit.io/v2.0/current?city=hudson&key=b3dbd1fd17094d04adab62cb4e9b8d23');

request.setMethod('GET');

request.setTimeout(30000);                    

HttpResponse response = http.send(request);

Ensure you have added following URL in remote site settings in Salesforce setup:
https://api.weatherbit.io/v2.0/current

You can also make use of Named Credentials in Salesforce.

Learn more about it here:
https://help.salesforce.com/articleView?id=sf.named_credentials_about.htm&type=5
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.