2

In angular I am capable to encapsulate all my query string parameters like this...

checkAvailability(context: string, data: string): Observable<ResCheckAvailability> {
let url = 'http://someurl'
let params = new HttpParams();
params = params.append(context, data);
return this.http.get(url,{params: params})
  .map(res => <ResCheckAvailability> res)
  .do(dataReceived => console.log(dataReceived))
}

Is there some how to do something elegant like this using apisauce ? or I will need to do something like...

import { create } from 'apisauce'
export const CheckAvailability = async (context, data) => {
  return api.get('http://someurl?context=' + context + '&data=' + data)
}

3 Answers 3

3

ApiSauce helps you to set the query string, You just have to pass the data as and it will append it at the last of the api.

For Example, You want to make a GET request on URL = http://someBaseUrl/path?context=2&data=3

import {create} from 'apisauce'
const baseUrl = 'http://somebaseUrl'
const path = 'path'
//initialising api sauce
const api = create({
baseUrl,
headers: {
//your random headers},
timeout: 10000
 })
//it will generate the above url
api.get(path, {
  params: {
    data: 3,
    context: 2
 }})

If you want to make POST, PUT request on the server with payload on the same url(which contains the query)

{ username: 'testingUsername' }

// I am skipping the initialisation part as it would be same for both
const data = {
  username: 'testingUsername' 
}
const params= { 
  params: {
    context: 2,
    data: 3
   }
}
api.post(path, data, params);
Sign up to request clarification or add additional context in comments.

Comments

0

This should do the job.

api.get(
  'http://someurl.com/endpoint',
  { context: 'context' , data: 'data' },
  { headers: { Authorization: token, ... } }
)

Comments

0

Updated answer to Supermacy's:

In order to pass query strings you shouldn't pass them in an params object.

For a GET request to http://someBaseUrl/path?context=2&data=3 you should do the following:

import {create} from 'apisauce'
const baseUrl = 'http://somebaseUrl'
const path = 'path'
//initialising api sauce
const api = create({
baseUrl,
headers: {
//your random headers},
timeout: 10000
 })
//it will generate the above url
api.get(path, {
    data: 3,
    context: 2
})

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.