#๐Ÿ”’ Request failed with status code 422

35 messages ยท Page 1 of 1 (latest)

agile thistle
#

when i try to click on scan all files I get this error.

thin fjordBOT
#

@agile thistle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

agile thistle
#

How can I resolve this issue? I can provide any code snippet you want... just tell me where is the error occuring..
I am using react and postgreSQL with fastAPI

#
@app.get("/scan-files/{user_id}", response_model=List[ScanResult])
async def scan_files(user_id: int, db: Session = Depends(SessionLocal)):
    user_files = db.query(UserFile).filter(UserFile.user_id == user_id).all()
    if not user_files:
        raise HTTPException(status_code=404, detail="No files found for the user")

    scan_results = []
    for db_file in user_files:
        with tempfile.TemporaryDirectory() as temp_dir:
            temp_file_path = os.path.join(temp_dir, db_file.fname)
            with open(temp_file_path, "wb") as temp_file:
                temp_file.write(db_file.data)

            try:
                virus_scan_task = asyncio.create_task(scan_file_with_clamav(temp_file_path))
                stego_scan_task = asyncio.create_task(check_steganography(temp_file_path))
                virus_scan_result, stego_scan_result = await asyncio.gather(virus_scan_task, stego_scan_task)

                # Interpret the scan results
                has_virus = virus_scan_result["status"] == "infected"
                has_steganography = stego_scan_result["status"] == "infected"

            except Exception as e:
                logger.error(f"Error scanning file {db_file.fname}: {e}")
                has_virus = False
                has_steganography = False

            scan_results.append({
                "file_id": db_file.id,
                "file_name": db_file.fname,
                "has_virus": has_virus,
                "has_steganography": has_steganography,
            })

    return scan_results
mental bridge
#

422 = Unprocessable Entity, so there's a problem with the POST data

agile thistle
#

that means the .js part of the code

mental bridge
#

the post data fails the validation so that endpoint code doesn't get to run

#

oh wait

#

this is a GET endpoint

agile thistle
#

yes

mental bridge
#

does it run to the point where you're returning the list?

#

how is ScanResult defined? Do you get a response body? what about errors in the server logs?

agile thistle
#

i am unable to check for its functionality because it asks for two parameters

#

idk what local_kw is but it is a required field...

#

i wanted that it would just take user_id and scan all the files of that user

mental bridge
#

why not just use Postman or another HTTP client to check?

agile thistle
#

idk how to use that

#

can you just guide me through the error and how to resolve it

agile thistle
wet mango
# agile thistle

Not sure if I understood this so please correct me. You made this API endpoint but you don't know what local_kw is and why its a required field?

agile thistle
wet mango
wet mango
#

No no

#

From client side

#

what did you send to your API?

wet mango
agile thistle
#
const handleScanAll = async () => {
    if (!user_id || !token) return; // Check if user_id and token are available

    setIsLoading(true); // Show loading indicator
    setScanResults([]); // Clear previous results
    setErrorMessage(''); // Clear any existing error message

    try {
      const response = await axios.get(`http://127.0.0.1:8000/scan-files/${user_id}`, {
        headers: {
          'Authorization': `Bearer ${token}`,
        },
      });

      setScanResults(response.data); // Update state with new scan results
    } catch (error) {
      console.error('Error scanning files:', error || error.response.data);
      setErrorMessage('Failed to scan the files. Please try again.'); // Set error message
    } finally {
      setIsLoading(false); // Hide loading indicator
    }
  };
agile thistle
#

or will it?

#

@wet mango

thin fjordBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.