2

Is it the recommended way to pass a JSON string as a parameter value in a REST API? This is the data which I am trying to send :

http://127.0.0.1:8000/v1/product/?productName=&metrics={"memory":2,"disk_space":10}

Here, is it ok to pass the value of metrics as JSON value?

Initially, I tried passing the metrics JSON value in the body. As it is not supported/recommended standard I have removed it.

Neeraj Wadhwa
  • 645
  • 2
  • 7
  • 18
Anish
  • 4,262
  • 6
  • 36
  • 58
  • Maybe it will help you http://dotnetcodeforu.blogspot.com/2013/12/pass-json-string-data-to-rest-service.html – munsifali Jun 14 '18 at 05:48

3 Answers3

3

Is it the recommended way to pass a JSON string as a parameter value in a REST API?

REST is an architectural style and it doesn't enforce (or even define) any standards for passing JSON string as a parameter value.


If you want to send JSON in the query string, you must URL encode it first:

/v1/products?productName=&metrics=%7B%22memory%22%3A2%2C%22disk_space%22%3A10%7D

Alternativelly, you could redesign the parameters to be like:

/v1/products?productName=&metrics.memory=2&metrics.diskSpace=10

If the URL gets too long (or the query gets too complex to be expressed in the query string), you may want to consider POST instead of GET, and then send the JSON in the request payload:

POST /v1/products/search HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "productName": "foo",
  "metrics": {
    "memory": 2,
    "diskSpace": 10
  }
}
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

Sending JSON values in GET request is not recommended. You can do it but metrics json can be long and anybody can read the content.

You should use POST request to send parameters such as productName and metrics in the body.

You should refer to this answer for detailed explanation.

Pramod
  • 1,376
  • 12
  • 21
  • Actually this should be GET request because it is not inserting any data to database so no issue with the reading the content by others . – Anish Jun 14 '18 at 05:49
0

For use Content-Type Application Json Please use below line

request.AddParameter("application/json", "{\n\t\"lastName\":\"gaurav.sablok@agarwalpackers.com\"\n}", ParameterType.RequestBody);

This is used with "RestSharp" name spaces

Zoe
  • 27,060
  • 21
  • 118
  • 148
Diwakar
  • 9
  • 1