Pass JSON object within Rest URL

Pass JSON object within Rest URL

I'm trying to test a Rest backend function that consumes a JSON object. The backend code is written in Python. I've tried several iterations when typing the URL in my browser during testing, but, I typically get a 400 error which leads me to think that I'm not formatting the URL correctly. For example, I've tried:

https://mybackend.url/?id=dev&reg="{ \"key\": \"some key\" }"

And other incantations. I feel like I'm not escaping the string correctly.

How can I type a Rest URL that passes a JSON object as a parameter?

Answer

You could either URL encode it e.g.,%7B%22key%22%3A%22some%20key%22%7D — https://mybackend.url/?id=dev&reg=%7B%22key%22%3A%22some%20key%22%7D

Or better base64 encode it from the client e.g., https://mybackend.url/?id=dev&reg=eyJrZXkiOiAic29tZSBrZXkifQ==

In both cases, you will have to decode the encoding in your python server. Unfortunately, browsers don’t automatically encode complex strings like JSON in query parameters.

Enjoyed this question?

Check out more content on our blog or follow us on social media.

Browse more questions