3

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.

1

3 Answers 3

4

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
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Cassio for your input
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.

2 Comments

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 .
Other issues with GET, including string length limitations. So no, GET should not be used. POST is almost always better and more maintainable.
0

For use Content-Type Application Json Please use below line

request.AddParameter("application/json", "{\n\t\"lastName\":\"[email protected]\"\n}", ParameterType.RequestBody);

This is used with "RestSharp" name spaces

1 Comment

[email protected] you can edit and put your values

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.