Bad advice for the "perfect" REST API

Hello!



Why is 'ideal' written in quotation marks ?!

, " ", , β€” )



Probably many of us have made REST APIs, or have used someone else's ready-made. Let's take a look at the "incredible" tricks that will help you make your API a cut above the others.



White belt



All values ​​in json are string and not otherwise!



Well, let's take the simplest object:



{
  "stringValue" : "value",
  "intValue": 123
}


That's why 123 is here to set a number, why such confusion? Let it be a string, the deserializer will figure it out:



{
  "stringValue" : "value",
  "intValue": "123"
}


, ? … ?



{
  "stringValue" : "value",
  "intValue": "123",
  "complexValue": {
    "key": "value"
  }
}


… ! -:



{
  "stringValue" : "value",
  "intValue": "123",
  "complexValue": "{
    \"key\": \"value\"
  }"
}


? ? , ? , !



{
  "stringValue" : "value",
  "intValue": "123",
  "complexValue": "{
    \"key\": \"value\",
    \"anotherComplexValue\": {
      \"superKey\": \"megaValue\"
    }
  }"
}


, ! ! !… ? ? , , . complexValue ? , , , , .



"Key": Value β€” , ...



2-3 , ? :



[
  25000, 
  "", 
  {
    "key1": "value1",
    "key2": "value2"
  }
]


! ? ! ? . , !



[
  25000, 
  "", 
  "{
    \"key1\": \"value1\",
    \"key2\": \"value2\"
  }"
]


! ! , , 5 :



[
  25000, 
  "",
  [
    "value1",
    "value2"
  ] 
]


! json , !







: β€” , ?



[
  "",
  "[
    \"value1\",
    \"value2\"
  ]",
  "25000"
]


? 3- ? , , , . ? 25000, , . ? ? β€” , !



. …



- . , , . :



{
  "queryType": "select",
  "table": "lyudi",
  "where": "name =  AND zarplata > 15000"
}


! ! ? , , )

, ?

, !

… ! :



{
  "query": "select * from lyudi where name =  AND zarplata > 15000"
}


! ? ? ? , . ORM? ? -. MSSQL .





""! -



: API ? : ! ! rest' . : , ! !



?!

, , , "", , , , )



, api vpn , : .





- , ! -!



JSON



. , ! , - :



{}


:



{
  "key1": "value1",
  "key2": 2
}


:



{
  "key1": "value1",
  "key2": 2,
  "key3": {
    "123": 456
  }
}


, :



{
  "objectAsArray": ["Vasya", 123, 456, "Piter"]
}


! !





! JSON : VARCHAR(MAX). !



!



dbf, ? !



{
  "data": "Vasya     123  456  Piter                  "
}


10 , 5 , 5 , 20 . , ! , , !





!



β€” ? , , !



{
  "data": [56, 61, 73, 79, 61, 20, 20, 20, 20, 20, 31, 32, 33, 20, 20, 34, 35, 36, 20, 20, 50, 69, 74, 65, 72, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
}


! :



{
  "data": "[56, 61, 73, 79, 61, 20, 20, 20, 20, 20, 31, 32, 33, 20, 20, 34, 35, 36, 20, 20, 50, 69, 74, 65, 72, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]"
}


, !



P.S. "" P.S. ...



If it seemed to you that the author exaggerates and invents, then ... The author would like it to be so. However, I had to deal with each of these cases. Let's respect each other and do good, and not some nonsense) Thank you for your attention, I hope in some places it was not only sad, but also fun!




All Articles