简体   繁体   English

通过Ruby发布时出错。 邮递员工作得很好

[英]Error when POST ing through Ruby. Postman works fine

I have a strange issue when trying to POST to a third party website. 尝试发布到第三方网站时遇到一个奇怪的问题。

When testing using Postman, I get a correct response. 使用Postman进行测试时,我得到了正确的答复。 However, when trying the same POST via Ruby code, I get a cryptic HTML response page from the website. 但是,当通过Ruby代码尝试相同的POST时,我从网站上获得了一个模糊的HTML响应页面。 HTTP Response code is 200. It's just that the website's internal logic throws an error, which should'nt happen if I'm sending the exact same request via code than the request I'm sending via Postman. HTTP响应代码为200。这只是网站的内部逻辑引发错误,如果我通过代码发送与通过Postman发送的请求完全相同的请求,则不会发生此错误。

Url is: http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias 网址是: http//www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias

The POST can be generated in the browser when choosing month ("mes") and day ("dia") in the dropboxes shown in that webpage. 在该网页显示的下拉框中选择月份(“ mes”)和日期(“ dia”)时,可以在浏览器中生成POST。 I have also inspected the network call in this case in the browser console and can find nothing funny. 在这种情况下,我也已经在浏览器控制台中检查了网络呼叫,发现没有什么好笑的。

My code comes straight from the one generated by Postman. 我的代码直接来自于Postman生成的代码。 I have also tried HTTParty gem with the same error response 我也尝试了HTTParty gem具有相同的错误响应

require 'uri'
require 'net/http'

url = URI("http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(url)
request["cache-control"] = 'no-cache'
request["content-type"] = 'application/x-www-form-urlencoded'
request["postman-token"] = '3ba1963c-2874-89c2-5e4d-e5be2c13a560'
request.body = "mes=05&anho=2016"

response = http.request(request)
puts response.read_body

A correct response should show an HTML table filled with values. 正确的响应应显示一个填充有值的HTML表。 Instead I'm getting an HTML error page. 相反,我得到了一个HTML错误页面。

Any help figuring out the issue would be appreciated. 任何帮助解决该问题的帮助,将不胜感激。

Edit: the HTML response is not really relevant, since it is a business logic error, not an HTTP error, but here it is: 编辑:HTML响应不是真正相关的,因为它是业务逻辑错误,而不是HTTP错误,但是这里是:

The thing is: this internal logic error is being triggered because something is different when sending the POST request via code than when sending it via Postman, and I can't figure out what. 问题是:触发了此内部逻辑错误,因为通过代码发送POST请求与通过邮递员发送POST请求时有所不同,我不知道是什么。

"\\r\\n\\r\\n.:: Pagina de Errores ::.\\r\\n\\r\\n\\r\\n\\r\\nBODY {font-style:normal;font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif;}\\r\\nH1 {font-size:16pt;color:Navy;}\\r\\nA {color:Navy;}\\r\\n.msg {font-style:bold;font-size:14pt;}\\r\\n.error {font-style:bold;font-size:14pt;color:Red;}\\r\\n.datos {font-size:12pt;}\\r\\n.soluc {font-size:12pt;}\\r\\n\\r\\n\\r\\nLa aplicación ha retornado el siguiente problema :\\r\\n “ \\ r \\ n \\ r \\ n。:: Pagina de Errores ::。\\ r \\ n \\ r \\ n \\ r \\ n \\ r \\ nBODY {font-style:normal; font-size:10pt; font-family :Verdana,Arial,Helvetica,sans-serif;} \\ r \\ nH1 {font-size:16pt; color:Navy;} \\ r \\ nA {color:Navy;} \\ r \\ n.msg {font-style:bold ; font-size:14pt;} \\ r \\ n.error {font-style:bold; font-size:14pt; color:Red;} \\ r \\ n.datos {font-size:12pt;} \\ r \\ n .soluc {font-size:12pt;} \\ r \\ n \\ r \\ n \\ r \\ nLaaplicaciónha retornado el siguiente problema:\\ r \\ n

\\r\\n \\ r \\ n
\\r\\n\\r\\n \\ r \\ n \\ r \\ n
\\r\\n\\r\\nAcción a realizar :\\r\\n\\r\\n\\r\\nPor favor intentente nuevamente realizar la operación, si el problema persiste, avisar a nuestro webmaster o\\r\\ncomunicarse con Atenci\\xF3n a Usuarios.\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n\\r\\n(function(){var f5_cspm={f5_p:'NEHEKPGFEEIGMFMPAJJJKDPGKDEIIJJIDBONLBJECPDLCCOBKCPONGDHNEIJOKPPCGMBMAGEAADECGEHHJAAAPLKAANKMODHPLFBCJKHMMCPOAKONNKGFELHONBMHBIO',setCharAt:function(str,index,chr){if(index>str.length-1)return str;return str.substr(0,index)+chr+str.substr(index+1);},get_byte:function(str,i){var s=(i/16)|0;i=(i&15);s=s*32;return((str.charCodeAt(i+16+s)-65)<<4)|(str.charCodeAt(i+s)-65);},set_byte:function(str,i,b){var s=(i/16)|0;i=(i&15);s=s*32;str=f5_cspm.setCharAt(str,(i+16+s),String.fromCharCode((b>>4)+65));str=f5_cspm.setCharAt(str,(i+s),String.fromCharCode((b&15)+65));return str;},set_latency:function(str,latency){latency=latency&0xffff;str=f5_cspm.set_byte(str,40,(latency>>8));str=f5_cspm.set_byte(str,41,(latency&0xff));str=f5_cspm.set_byte(str,35, \\ r \\ n \\ r \\ n请注意以下事项:\\ r \\ n \\ r \\ n \\ r \\ n请优先关注新的要进行操作的问题,请务必坚持,在任何情况下,请使用网站管理员或其他任何网站管理员 。 function(str,index,chr){if(index> str.length-1)return str; return str.substr(0,index)+ chr + str.substr(index + 1);},get_byte:function(str ,i){var s =(i / 16)| 0; i =(i&15); s = s * 32; return((str.charCodeAt(i + 16 + s)-65)<< 4)|(str .charCodeAt(i + s)-65);},set_byte:function(str,i,b){var s =(i / 16)| 0; i =(i&15); s = s * 32; str = f5_cspm .setCharAt(str,(i + 16 + s),String.fromCharCode((b >> 4)+65)); str = f5_cspm.setCharAt(str,(i + s),String.fromCharCode((b&15)+ 65)); return str;},set_latency:function(str,latency){latency = latency&0xffff; str = f5_cspm.set_byte(str,40,(latency >> 8)); str = f5_cspm.set_byte(str,41, (latency&0xff)); str = f5_cspm.set_byte(str,35, 2);return str;},wait_perf_data:function(){try{var wp=window.performance.timing;if(wp.loadEventEnd>0){var res=wp.loadEventEnd-wp.navigationStart;if(res<60001){var cookie_val=f5_cspm.set_latency(f5_cspm.f5_p,res);window.document.cookie='f5avr1032272937aaaaaaaaaaaaaaaa='+encodeURIComponent(cookie_val)+';path=/';}\\nreturn;}}\\ncatch(err){return;}\\nsetTimeout(f5_cspm.wait_perf_data,100);return;},go:function(){var chunk=window.document.cookie.split(/\\s*;\\s*/);for(var i=0;i" 2); return str;},wait_perf_data:function(){尝试{var wp = window.performance.timing; if(wp.loadEventEnd> 0){var res = wp.loadEventEnd-wp.navigationStart; if(res <60001 ){var cookie_val = f5_cspm.set_latency(f5_cspm.f5_p,res); window.document.cookie ='f5avr1032272937aaaaaaaaaaaaaaaaaa ='+ encodeURIComponent(cookie_val)+'; path = /';} \\ nreturn;}} \\ ncatch(err) {return;} \\ nsetTimeout(f5_cspm.wait_perf_data,100); return;},go:function(){var chunk = window.document.cookie.split(/ \\ s *; \\ s * /); for(var i = 0; i“

You need to probably use a GET request to get the table. 您可能需要使用GET请求来获取表。 The server is not responding on the POST request because it has not been configured to respond to it. 服务器未响应POST请求,因为尚未将其配置为响应该请求。

The solution is to use: 解决方案是使用:

uri = URI('http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias')
request = Net::HTTP::Get.new(uri)

instead of the code with Post.new 而不是Post.new的代码

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

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