Standardized way to serialize JSON to query string?

后端 未结 4 1034
不思量自难忘°
不思量自难忘° 2020-11-27 04:16

I\'m trying to build a restful API and I\'m struggling on how to serialize JSON data to a HTTP query string.

There are a numb

4条回答
  •  情深已故
    2020-11-27 04:51

    URL-encode (https://en.wikipedia.org/wiki/Percent-encoding) your JSON text and put it into a single query string parameter. for example, if you want to pass {"val": 1}:

    mysite.com/path?json=%7B%22val%22%3A%201%7D
    

    Note that if your JSON gets too long then you will run into a URL length limitation problem. In which case I would use POST with a body (yes, I know, sending a POST when you want to fetch something is not "pure" and does not fit well into the REST paradigm, but neither is your domain specific JSON-based query language).

提交回复
热议问题