0

I am new to reactjs.I am doing a post call but not sure, how to pass a boolean value in url as a query param in reactjs. For eg: http://www.abx.com?example=true. How do I pass this example in post api call. Endpoint: API_SAMPLE: "/sample",

post call:

postCall() {
    const config = {
      headers: {
        accept: "application/json",
        "Content-Type": "application/json",
      },
    };
    const data = {
      product: {
        body
      },

    };
    return http
      .post(this.API.API_SAMPLE, data, config)
      .then((response) => {
        return response.data;
      })
      .catch((error) => {
        throw error;
      });
  }

i want to add a boolean value in my query param, how will i do that

1
  • Umm. Type in into the URL. Commented May 4, 2020 at 13:54

2 Answers 2

2

You can use template literals to pass the variable to your URL.

const example = true;
const url = `http://abx.com/sample?example=${example}`

or use it this way:

return http.post(`${this.API.API_SAMPLE}?example=${example}`, data, config) {...}
Sign up to request clarification or add additional context in comments.

3 Comments

i have updated my question, Can you plz have a look how I'll be do it in my case
Replace this.API.API_SAMPLE with url variable that you create as I showed you
is there any way to add this query param in endpoint: API_SAMPLE: "/sample"
1

wether you are using POST or GET you will have to modify the URL. You can do this "dynamically" or you can use something like URLSearchParams: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

var URL = "http://example.com/search";
var searchParams = new URLSearchParams(URL);

searchParams.append("example", "true");

searchParams.toString() // "http://example.com/search?example=true";

Just make sure you support the correct browsers: https://caniuse.com/#search=urlsearchparams

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.