This is the code for the main function for FastAPI, in which can be seen the different endpoints that I have developed. After enabling the library, we can find the first two endpoints (simple ones) that works perfectly both on my local Docker as on CloudRun.
After that we can find the endopints that get data from a Payload. Those works perfectly on ly local Docker but when deployed on CloudRun those get the error Expecting value: line 1 column 1 (char 0)
, showing the classic Google 400 page.
### FASTAPI ACTIVATION ###
from fastapi import FastAPI, Request
app = FastAPI(
title="API Test",
version="1.0.0")
########################
### SIMPLE ENDPOINTS ###
########################
# Those Works both on Docker as in CloudRun
@app.get('/endpoint_01')
def endpoint_01_func():
return {'Works':'Fine'}
@app.get('/endpoint_02/{param01}')
def endpoint_02_func(param01):
upper_txt = param01.upper()
return {"Works":"Fine",
"Original":param01,
"Edited":upper_txt}
##############################
### ENDPOINTS WITH PAYLOAD ###
##############################
# Works on Docker but NOT ON CLOUDRUN
# Creation of the Class
from pydantic import BaseModel
class Sample_Class(BaseModel):
value_01:str
value_02:str
# Creation of Endpoints
@app.get("/endpoint_03_V1")
def payload_read_V1(payload:Sample_Class):
value_01 = payload.value_01
value_02 = payload.value_02
return {
"V1":f"{value_01}",
"V2":f"{value_02}"}
@app.get("/endpoint_03_V2")
async def payload_read_V2(payload:Sample_Class):
value_01 = payload.value_01
value_02 = payload.value_02
return {
"V1":f"{value_01}",
"V2":f"{value_02}"}
To provide a complete overview of the container, here you can find the Docker code (I follow the official guidelines).
FROM python:3.11.2-slim-buster
RUN apt-get update && \
apt-get install -y build-essential && \
apt-get clean
WORKDIR /code
COPY ./app /code/app
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
The following code relates to the code that i use to make the request to the API in Python (the other endpoints works perfectly). I have tried different ways, but the problem is always the same, in my local machine everything works, but not when deployed on CloudRun generating the error Expecting value: line 1 column 1 (char 0)
.
import requests
import json
payload_xx = {
"value_01":"Stack",
"value_02":"Overflow"}
def request_cloud(payload):
url = 'https://example-domain.com/endpoint_03_V1'
request_send = requests.get(url, json=payload)
json_data = json.loads(request_send.text)
return json_data
request_cloud(payload_xx)
One possibility could be the presence of the HTTPS protocol, or the delivery of the payload on the cloud. But i haven't found anything on that aspect.
Thank you in advance for your help and patience!
I have solved this issue by rewriting the APIs using Flask. The code is similar, but once uploaded on Cloud Run everything works perfectly.