1
votes

enter image description here

Hi i am trying to pass base64 image to post api request but getting error please help i am new to programming

import requests
import json, base64
final_list = []


    
def vehicle_damage(img):
    url = "https://vehicle-damage-assessment.p.rapidapi.com/run"
    payload = {
    "draw_result": True,
    "image": img
}
    headers = {
    "content-type": "application/json",
    "X-RapidAPI-Host": "vehicle-damage-assessment.p.rapidapi.com",
    "X-RapidAPI-Key": "bd31338435msha00c21a34e0a18dp10bb9cjsn067429eb078f"
    }
    response = requests.request("POST", url, json=payload, headers=headers)
    res = response
    json_data = json.loads(response.text)
    anoted_url = (json_data['output_url'])
    for item in (json_data['output']['elements']):
        damage_cat = (item['damage_category'])
        damage_loc = (item['damage_location'])
        damage_per = (item['score'])
        damage_per = damage_per*100
        final_list.append(damage_cat)
        final_list.append(damage_loc)
        final_list.append(damage_per)

image = open("C:\\Users\\286303\\Pictures\\Car Damaged\\Damaged\\052a5e6f08d199f88379964c4e4d6196--car-body-repairs-audi-a.jpg", 'rb')        
image_read = image.read()
image_64_encode = base64.b64decode(image_read)
print(image_64_encode)
vehicle_damage(image_64_encode)
1
you have to modify the content type Content-Type":"multipart/form-data"Harsha Biyani

1 Answers

0
votes

you need to change it like this:

def vehicle_damage(img: bytes):
    ...
    payload = {"draw_result": True, "image": str(img)}
    ...

make 'img' as bytes in function's arguments and make it string when put it in your payload dict.