我使用nginx作为几个tomcats前面的负载balencer.在我的传入请求中,我编码了查询参数.但是当请求到达tomcat时,参数被解码:
传入nginx的请求:
curl -i "http://server/1.1/json/T;cID=1234;pID=1200;rF=http%3A%2F%2Fwww.google.com%2F"
Run Code Online (Sandbox Code Playgroud)
传入tomcat的请求:
curl -i "http://server/1.1/json/T;cID=1234;pID=1200;rF=http:/www.google.com/"
Run Code Online (Sandbox Code Playgroud)
我不希望我的请求参数被转换,因为在那种情况下我的tomcat会抛出405错误.
我的nginx配置如下:
upstream tracking {
server front-01.server.com:8080;
server front-02.server.com:8080;
server front-03.server.com:8080;
server front-04.server.com:8080;
}
server {
listen 80;
server_name tracking.server.com;
access_log /var/log/nginx/tracking-access.log;
error_log /var/log/nginx/tracking-error.log;
location / {
proxy_pass http://tracking/webapp;
}
}
Run Code Online (Sandbox Code Playgroud)
在我当前的apache负载均衡器配置中,我有AllowEncodedSlashes指令来保留我的编码参数:
AllowEncodedSlashes NoDecode
Run Code Online (Sandbox Code Playgroud)
我需要从apache转移到nginx.
我的问题与这个问题完全相反:避免nginx在proxy_pass上转义查询参数
我正在设计一个RESTful API.
一项服务应为多个键值对提供查询功能.例如,客户端可以使用一个HTTP GET请求查询不同的产品和相关数量.
客户想要使用金额44和产品2查询产品1,金额为55.我实际上不希望我的URI看起来像这样:
/produkt?productId1=1&productquantity1=44&productId2=2&productquantity2=55
Run Code Online (Sandbox Code Playgroud)
因为我不知道有多少产品被查询.
或者像这样:
/produkt?product=1,44&product=2,55
Run Code Online (Sandbox Code Playgroud)
因为客户怎么能知道在逗号之前有productId和逗号之后的数量.
有没有人有其他解决方案?或者,提供使用一个请求查询多个产品的可能性是不是RESTful?是否更好地提供仅查询一个具有相关数量的产品的可能性,如果客户想要查询更多产品,他们应该发送更多请求?
我正在用Python编写测试脚本来检查我的PHP应用程序的输出,我遇到了Python urlparse.parse_qs()函数的问题.GET字符串分隔符(AFAIK)是一个&符号.该函数(据我所知)应该将GET字符串拆分为Python字典,因此输出count=2&offset=5&userID=1应为:
{'count': ['2'], 'userID': ['1'], 'offset': ['5']}
Run Code Online (Sandbox Code Playgroud)
它是.但是当我尝试在GET中传递CSV(用分号分隔)时ids=5;15;3,我得到以下内容:
[('3', ''), ('15', ''), ('ids', '5')]
Run Code Online (Sandbox Code Playgroud)
我认为有效输出应如下所示:
{'ids': ['5;15;3']}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?这条线看起来像这样:
args = urlparse.parse_qs(sys.argv[2], keep_blank_values=True)
Run Code Online (Sandbox Code Playgroud) 我正在以这种格式调用Web服务:
HTTP://some.server/rest/resource; A = B
它有效,但这是有效的吗?我见过了; 用作替代&但从未见过这样的网址.我一直在寻找答案,但没有找到有效的答案.如果有效,这种网址的含义是什么?
哪些库/调用可用于处理包含分号的查询字符串与parse_qs不同?
>>> urlparse.parse_qs("tagged=python;ruby")
>>> {'tagged': ['python']}
Run Code Online (Sandbox Code Playgroud)
我正在使用StackExchange API来搜索标记的问题.
搜索的布局是这样的,标签用分号分隔:
/2.1/search?order=desc&sort=activity&tagged=python;ruby&site=stackoverflow
与API交互就好了.当我想测试调用时,特别是当使用httpretty来模拟HTTP时,会出现问题.
在引擎盖下,httpretty使用urlparse.parse_qspython标准库来解析查询字符串.
>>> urlparse.parse_qs("tagged=python;ruby")
{'tagged': ['python']}
Run Code Online (Sandbox Code Playgroud)
显然这不太好用.这是一个小例子,这里是httpretty的一小部分(在测试环境之外).
import requests
import httpretty
httpretty.enable()
httpretty.register_uri(httpretty.GET, "https://api.stackexchange.com/2.1/search", body='{"items":[]}')
resp = requests.get("https://api.stackexchange.com/2.1/search", params={"tagged":"python;ruby"})
httpretty_request = httpretty.last_request()
print(httpretty_request.querystring)
httpretty.disable()
httpretty.reset()
Run Code Online (Sandbox Code Playgroud)
我想使用来自httpretty的机器,但需要一个解决方法parse_qs.我现在可以修补httpretty,但是很想看看还能做些什么.