This question already has answers here:
Ruby - net/http - following redirects
(6 answers)
Closed 17 days ago.
I'm currently simply trying to get a simple GET request working in Ruby, however, I'm seeing some strange behavior.
I have an Open Web Analytics application running with Docker and it is reachable at http://127.0.0.1:8080/.
I can reach the login site and everything works fine.
Now I want to do a GET request with Ruby to analyze the body of that request but I cannot get it to work, in other languages like Python or simple GET requests over the terminal it works fine. Why not with Ruby?
Here is my very basic Ruby code:
require 'net/http'
url = 'http://127.0.0.1:8080/'
uri = URI(url)
session = Net::HTTP.new(uri.host, uri.port)
response = session.get(uri.request_uri)
puts response.body
Which doesn't output anything. If I look into the NGINX logs from the container, I can see the request being made but there is no further redirection as with the other methods (see below).
172.23.0.1 - - [02/Feb/2023:20:02:59 +0000] "GET / HTTP/1.1" 302 5 "-" "Ruby" "-" 0.088 0.088 . -
If I do a simple GET over the terminal, it works:
GET http://127.0.0.1:8080/
will output the correct body, and in the NGINX logs I can see the following:
172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET / HTTP/1.1" 302 5 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -
172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET /index.php?owa_do=base.loginForm&owa_go=http%3A%2F%2F127.0.0.1%3A8080%2F& HTTP/1.1" 200 3200 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -
Doing it in Python with the following basic code also works and gives similar results as with the terminal GET version:
import requests
x = requests.get("http://127.0.0.1:8080/")
print(x.content)
What am I doing wrong?
Got it working with following redirects (see here):
begin
response = Net::HTTP.get_response(URI.parse(url))
url = response['location']
end while response.is_a?(Net::HTTPRedirection)
I tried a bunch of tutorials, here is an example of one of them
https://nickmccullum.com/build-facebook-bot-python-flask/
Request is send:
127.0.0.1 - - [02/May/2022 10:18:15] "GET /?hub.mode=subscribe&hub.challenge=646416116&hub.verify_token=YOUR_VERIFY_TOKEN HTTP/1.1" 200 -
But when trying to validate, the Webhook throws this error:
Validation of the callback URL or confirmation token failed. Confirm the information provided or try again later.
what could be the problem
I have this function:
if not (payload.get('password') == payload.get('password_cpy')):
abort(400)
when I run the app with flask run and debug is off, I get a pretty print of a BadRequest error which in raw shows the following info:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not
understand.</p>
And on terminal I see:
127.0.0.1 - - [30/Apr/2018 00:28:20] "POST /sign_in HTTP/1.1" 400 -
When I run it and debug is on, the error I get is:
werkzeug.exceptions.BadRequest
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy)
sent a request that this server could not understand.
And in terminal I see a 500 error:
127.0.0.1 - - [30/Apr/2018 00:29:27] "POST /sign_in HTTP/1.1" 500 -
Why do I get two different behaviors? Is there a way in which I can get the 400 error even in debug mode?
try to update your Flask1.0 to Flask1.0.1
I guess the bug has been fixed in Flask1.0.1 :)
https://github.com/pallets/flask/issues/2735
I have a strange issue with the TCP JSON-RPC server I've created in Python-2.7. I used the following code to build the server:
https://github.com/joshmarshall/jsonrpclib
I am communicating client to server from within the same local network. In the console window, I can connect to and run commands against the server from within Python. All is well there.
However, when I try to send JSON strings from a mobile app (in this case an iPad) I get an error on the server. I have also downloaded this tool in an attempt to send the JSON strings: http://www.simplecomtools.com/productcart/pc/downloads/tcptesttool.zip but with the same error result. The server is reporting a "Bad request syntax" error. I've tried several different strings - the displayed errors are:
192.168.1.107 - - [13/Oct/2012 09:48:17] code 400, message Bad request syntax ("{'jsonrpc':'2.0','method':'add','params':[3,6],'id':'8'}")
192.168.1.107 - - [13/Oct/2012 09:48:17] "{'jsonrpc':'2.0','method':'add','params':[3,6],'id':'8'}" 400 -
192.168.1.107 - - [13/Oct/2012 09:49:44] code 400, message Bad request syntax ('{"jsonrpc":"2.0","method":"add","params":[3,6],"id":"8"}')
192.168.1.107 - - [13/Oct/2012 09:49:44] "{"jsonrpc":"2.0","method":"add","params":[3,6],"id":"8"}" 400 -
192.168.1.107 - - [13/Oct/2012 09:50:49] code 400, message Bad request syntax ('{"jsonrpc":"2.0","method":"add","params":{"x":3,"y":6},"id":"8"}')
192.168.1.107 - - [13/Oct/2012 09:50:49] "{"jsonrpc":"2.0","method":"add","params":{"x":3,"y":6},"id":"8"}" 400 -
192.168.1.107 - - [13/Oct/2012 17:11:59] code 400, message Bad request syntax ("{'jsonrpc':'2.0', 'method':'add', 'params':{'x':3,'y':6}, 'id':8}")
192.168.1.107 - - [13/Oct/2012 17:11:59] "{'jsonrpc':'2.0', 'method':'add', 'params':{'x':3,'y':6}, 'id':8}" 400 -
I really have no idea why the server would think the request syntax is bad, and I feel a little silly even asking the question. Any ideas on what I could try to solve the syntax error?
In message 1 and 4, your client is not actually sending JSON; it is using ' to denote string boundaries, instead of ". While single quotes are supported by some JSON implementations, they are invalid according to the standard. Correct your client implementation to send actual JSON with "-delimited strings.
But the main problem is that you're not wrapping your messages into HTTP POST requests, but sending them raw. A proper JSONRPC request looks like:
POST / HTTP/1.0
Content-Length: 71
{"jsonrpc": "2.0", "params": [3, 6], "id": "er5qtdbz", "method": "pow"}
, but you're sending just the last line.
In Python, you can send a valid request with the following example program:
import json
try:
from urllib.request import urlopen
except ImportError: # Python<3
from urllib2 import urlopen
req = {"jsonrpc":"2.0","method":"add","params":[3,6],"id":0}
req_data = json.dumps(req).encode('utf-8')
u = urlopen('http://localhost:8080/', req_data)
print(u.read())
I have a Django app that's serving up a RESTful API using tasty-pie.
I'm using Django's development runserver to test.
When I access it via a browser it works fine, and using Curl also works fine:
curl "http://localhost:8000/api/v1/host/?name__regex=&format=json"
On the console with runserver, I see:
[02/Oct/2012 17:24:20] "GET /api/v1/host/?name__regex=&format=json HTTP/1.1" 200 2845
However, when I try to use the Python requests module (http://docs.python-requests.org/en/latest/), I get a 404 as the output:
>>> r = requests.get('http://localhost:8000/api/v1/host/?name__regex=&format=json')
>>> r
<Response [404]>
or:
>>> r = requests.get('http://localhost:8000/api/v1/host/?name__regex=&format=json')
>>> r
<Response [404]>
Also, on the Django runserver console, I see:
[02/Oct/2012 17:25:01] "GET http://localhost:8000/api/v1/host/?name__regex=&format=json HTTP/1.1" 404 161072
For some reason, when I use requests, it prints out the whole request URL, including localhost - but not when I use the browser, or curl.
I'm assuming this is something to do with the encoding, user-agent or request type it's sending?
I'm not very familiar with Requests, but I think your encoding idea might be sound. That is Requests might process the URL somehow? Perhaps instead of passing everything in the URL directly, try doing what Requests docs suggest:
request_params = { 'name_regex' : '', 'format' : 'json' }
r = requests.get( 'http://localhost:8000/api/v1/host/', params = request_params )