0

I have this.$route.query in which the object is:

{ next: "/api/o/authorize/?client_id=xxx", nonce: "bee", redirect_uri: "http://X.app.net/oauth/providers/appZ/callback", response_type: "code", scope: "read", state: "123" }

How to convert it to:

/api/o/authorize/?client_id=xxx&nonce=bee&redirect_uri=http://X.app.net/oauth/providers/appZ/callback&response_type=code&scope=read&state=123

Maybe I missed something and there is a method for this in Vue or Js?

2
  • That's it's in query means that the whole thing is a query, ?next=/api/o/authorize...&nonce=bee&.... Notice that a path in next needs to be URL encoded. Consider explaining your case. It's unclear why you expect next to become a path. Commented Jun 9, 2021 at 8:09
  • 1
    You'll probably have to build the URL yourself. Check out URLSearchParams and URL. They should handle all the URL encoding so you don't have to. Commented Jun 9, 2021 at 8:11

2 Answers 2

3

You can pipe plain, single-level objects through URLSearchParams to form correctly encoded query strings.

For example

// just an example to match your Vue code
this.$route = {
  query: { next: "/api/o/authorize/?client_id=xxx", nonce: "bee", redirect_uri: "http://X.app.net/oauth/providers/appZ/callback", response_type: "code", scope: "read", state: "123" }
}

const { next, ...params } = this.$route.query
const url = `${next}&${new URLSearchParams(params)}`

console.log(url)

Sign up to request clarification or add additional context in comments.

Comments

1

Define a computed property called query :

computed:{
  query(){
    let q=this.$route.query;
      return Object.keys(q).map(k=>`${k}=${q[k]}`).join('&').replace("next=","")
  }
}

example in js :

let q={ next: "/api/o/authorize/?client_id=xxx", nonce: "bee", redirect_uri: "http://X.app.net/oauth/providers/appZ/callback", response_type: "code", scope: "read", state: "123" }

let query=Object.keys(q).map(k=>`${k}=${q[k]}`).join('&').replace("next=","")

console.log(query)

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.