简体   繁体   中英

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. However, when trying the same POST via Ruby code, I get a cryptic HTML response page from the website. 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.

Url is: 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. 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. I have also tried HTTParty gem with the same error response

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. Instead I'm getting an HTML error page.

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:

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.

"\\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\\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, 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"

You need to probably use a GET request to get the table. The server is not responding on the POST request because it has not been configured to respond to it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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