简体   繁体   English

如何在REST API的URL中传递列表?

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

I am trying to pass a list of IDs to a REST api (apex ords). 我正在尝试将ID列表传递给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 : 我得到id为1的商品,但如果这样做:

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

I get a 500 Internal Server Error 我收到500内部服务器错误

How should I format my url so I can use the 1,2,3 list in a where id in (ids) in apex ords? 如何设置网址格式,以便可以在先端ords where id in (ids)中的where id in (ids)使用1,2,3列表?

this is a screenshot of ords if it can help : 如果可以的话,这是ords的屏幕截图:

在此处输入图片说明

That SQL won't work becase ORDS does not split the csv values out. 如果ORDS无法将csv值分开,则该SQL将无法正常工作。 So that sql as-is will be checking for id in ( '1,2,3') not id in ( 1,2,3) 这样sql原样将检查id in ( '1,2,3') id in ( 1,2,3)不是id in ( 1,2,3)

There's multiple ways to accomplish what the intent is. 有多种方法可以实现目的。

For example, using XMLTABLE 例如,使用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 这里还提到了其他方法: 在Oracle SQL中,将“ IN”子句与逗号分隔的字符串一起使用,该字符串来自逗号替换函数的输出

Here's an ORDS REST API doing exactly what you intend. 这是ORDS REST API的功能,完全可以满足您的需求。 在此处输入图片说明

In a URL, comma ',' has special meaning/purpose. 在URL中,逗号','具有特殊含义/目的。 It is to separate query arguments in url eg 它是将URL中的查询参数分开

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. 因此,服务器发现损坏的或不完整的键/值对时将抛出500错误。 It expects key=value pair after each comma. 期望在每个逗号后有键=值对。 To get around this we need to urlencode value eg 为了解决这个问题,我们需要urlencode值,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM