How do I pass a list in url for a REST api?

雨燕双飞 提交于 2019-12-24 23:08:33

问题


I am trying to pass a list of IDs to a REST api (apex ords).

I have a url like this :

https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/:ids

when I do :

https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1

I get the item with id = 1 but if I do :

https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1,2,3

I get a 500 Internal Server Error

How should I format my url so I can use the 1,2,3 list in a where id in (ids) in apex ords?

this is a screenshot of ords if it can help :


回答1:


That SQL won't work becase ORDS does not split the csv values out. So that sql as-is will be checking for id in ( '1,2,3') not id in ( 1,2,3)

There's multiple ways to accomplish what the intent is.

For example, using XMLTABLE

SELECT rownum,object_id
  FROM user_objects
 WHERE rownum IN (
   select (column_value).getstringval() csv_values
        FROM   
     xmltable(('"' || REPLACE(:ids, ',', '","')|| '"'))
 )

There are other ways mentioned here: Using the "IN" clause with a comma delimited string from the output of a replace() function in Oracle SQL

Here's an ORDS REST API doing exactly what you intend.




回答2:


In a URL, comma ',' has special meaning/purpose. It is to separate query arguments in url e.g

https://test.me/mypage?firstname=jon,lastname=doe,gender=m

So server is throwing 500 error as it finds corrupted or incomplete key/value pairs. It expects key=value pair after each comma. To get around this we need to urlencode value e.g

https://apex.oracle.com/***/apex/anime_keeper/ak/getAnimeList/1%2C2%2C3


来源:https://stackoverflow.com/questions/50429334/how-do-i-pass-a-list-in-url-for-a-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!