Is it possible to pass JSON in a URL?
For example, I have an array:
data = {
"name": "test",
"user_id": 1
}
I want to pass it in to a URL like:
http://example.com/jsonArray
Is it possible to pass JSON in a URL?
For example, I have an array:
data = {
"name": "test",
"user_id": 1
}
I want to pass it in to a URL like:
http://example.com/jsonArray
You can't send the JSON array directly, but you can prepare it in a way that allows it to be sent to your API. You turn your JSON into a string and encode it so it will be properly formatted for a GET.
// your JSON data
var data = '{"name":"Homer Simpson","status":"Suspended","disciplinary-dates":["2018-01-2","2018-02-14","2018-03-17"]}' ;
// take your JSON, turn it into a string and then encode it
var urlParams = encodeURIComponent(JSON.stringify(data)) ;
// your URL adding your JSON data as the value
var url = 'http://example.com/?params=' + urlParams ;
On the PHP side you will decode
// note: this code does not have any error checking - you should add it
// get your params
$params = $_GET['params'] ;
// decode the string
$decodedParams = urldecode($params) ;
// turn your string into an array
$wasJSONAsArray = json_decode($decodedParams, true) ;
// turn your string into a std object
$wasJSONAsStdObj = json_decode($decodedParams) ;