+ Start a Discussion
sevindusevindu 

how to pass query parameters in HttpRequest?

I am currently using following method to get authorization code
############################
public PageReference sync(){ 
        
        settings = ConstantContactSettings.getData();
        
        PageReference ref = new PageReference(ConstantContactConstants.AUTH_ENDPOINT+
                                             'client_id='+settings.ClientID__c+
                                             '&redirect_uri='+settings.Redirect_URL__c+
                                             '&scope='+ConstantContactConstants.SCOPE+
                                             '&response_type='+ConstantContactConstants.RESPONSE_TYPE);
        
        return ref;
    }
#######################

Issue is it refreshes the page. How can I use HttpGet Callout instead of this approach and how to add query parameters to endpoint URL?
Alan VillegasAlan Villegas
this.http.get(StaticSettings.BASE_URL).subscribe( (response) => this.onGetForecastResult(response.json()), (error) => this.onGetForecastError(error.json()), () => this.onGetForecastComplete() );
More click here (https://tattoodi.com/tiger-tattoos/)
mukesh guptamukesh gupta
Hi Sevindu,

you can  use below code:-
 
HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.openweathermap.org/data/2.5/weather?q='+city+'&APPID=#####################');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            return new List<String> {response.getBody()};
        }

if you fond this useful then, PLEASE MARK AS A BEST ANSWER!!
Deepali KulshresthaDeepali Kulshrestha
Hi sevindu,

Please refer these links, maybe it will help you

For HttpRequest Class:-
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_httprequest.htm

For Invoking HTTP Callouts :-

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_http.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi Sevindu,
According to other links:
You can configure it by importing the HTTP client module from the @angular/common/HTTP package.

import {HttpClientModule} from '@angular/common/http';

@NgModule({
  imports: [ BrowserModule, HttpClientModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
After that you can inject the HttpClient and use it to do the request.

import {HttpClient} from '@angular/common/http'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor(private httpClient: HttpClient) {
    this.httpClient.get('/url', {
      params: {
        appid: 'id1234',
        cnt: '5'
      },
      observe: 'response'
    })
    .toPromise()
    .then(response => {
      console.log(response);
    })
    .catch(console.log);
  }
}
For angular versions prior to version 4 you can do the same using the Http service.

The Http.get method takes an object that implements RequestOptionsArgs as a second parameter.

The search field of that object can be used to set a string or a URLSearchParams object.

An example:

 // Parameters obj-
 let params: URLSearchParams = new URLSearchParams();
 params.set('appid', StaticSettings.API_KEY);
 params.set('cnt', days.toString());

 //Http request-
 return this.http.get(StaticSettings.BASE_URL, {
   search: params
 }).subscribe(
   (response) => this.onGetForecastResult(response.json()), 
   (error) => this.onGetForecastError(error.json()), 
   () => this.onGetForecastComplete()
 );
The documentation for the Http class has more details. It can be found here and an working example here.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi