# Invalid base64 string returned from REST API

**URL:** <https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491>\
**Category:** Building Generative AI applications with Gradio\
**Created:** [August 8, 2023, 5:17am UTC](https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491 "2023-08-08T05:17:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anirban\_K](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@Anirban\_K](https://community.deeplearning.ai/u/Anirban_K)\
**Post date:** [August 8, 2023, 5:17am UTC](https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491/1 "2023-08-08T05:17:11Z")

</div>

Hello,  
I am using this code

import base64  
from io import BytesIO  
from PIL import Image

API\_URL = “[https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5](https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5)”  
def get\_completion(inputs, parameters=None,ENDPOINT\_URL=API\_URL):  
headers = {  
“Authorization”: f"Bearer {API\_TOKEN}",  
“Content-Type”: “application/json”  
}  
data = { “inputs”: inputs }  
if parameters is not None:  
data.update({“parameters”: parameters})  
response = requests.request(“POST”,  
ENDPOINT\_URL, headers=headers,  
data=json.dumps(data)  
)  
return response.content

Once I get the response, I am trying to convert it into PIL Image object, but I find the base64 string returned is invalid. (Checked by putting the returned base64 string on online conversion site)  
My code -  
result = get\_completion(prompt, parameters=None,ENDPOINT\_URL= API\_URL)  
print(result)  
#b64 = result.split(“,”)[1]

im\_bytes = base64.b64decode(result)  
im\_file = BytesIO(im\_bytes)  
img = Image.open(im\_file)  
img.show()

Can someone tell me what is the mistake I am doing. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Anirban\_K](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@Anirban\_K](https://community.deeplearning.ai/u/Anirban_K)\
**Post date:** [August 8, 2023, 7:20am UTC](https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491/2 "2023-08-08T07:20:04Z")

</div>

Just co clarify : I am getting this error - UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte  
if i use return json.loads(response.content.decode(“utf-8”)) - without the decode, its giving invalid base64 string.  
Also I tried using the same exact API URL and Key as in the examples, but that gives “Timeout error” , its going to some AWS hosted URL or something.  
I could however, do the NLP examples using same API Token and giving URL to the HuggingFace models , that worked fine.  
There is another alternative, to run it locally, using DiffusionPipeline, which I tried (it downloads a super huge model, 3 GB) but it also gives same issue, invalid base64 string. Please help.

---

<div class="post-metadata">

**Author:** ![Adam\_Hjerpe](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Adam\_Hjerpe](https://community.deeplearning.ai/u/Adam_Hjerpe)\
**Post date:** [August 19, 2023, 11:31am UTC](https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491/3 "2023-08-19T11:31:41Z")

</div>

I got the same error message, as you @Anirban_K, when I use the API endpoint that you specify above.

I solved it by modifying the course code to,

```auto
def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_TTI_BASE']):
    headers = {
      "Authorization": f"Bearer {hf_api_key}",
      "Content-Type": "application/json"
    }   
    data = { "inputs": inputs }
    if parameters is not None:
        data.update({"parameters": parameters})
    response = requests.request("POST",
                                ENDPOINT_URL,
                                headers=headers,
                                data=json.dumps(data))
    contents = response.content
    return contents # (no decoding)

prompt = "a dog in a park"
result = get_completion(prompt)

# Assuming 'result' contains the binary data of the image in PNG format
image = Image.open(io.BytesIO(result))

# Convert the image binary data to base64 encoding
image_data = io.BytesIO()
image.save(image_data, format="PNG")
image_base64 = base64.b64encode(image_data.getvalue()).decode("utf-8")
html_code = f'<img src="data:image/png;base64,{image_base64}" />'
IPython.display.HTML(html_code)

```

---

<div class="post-metadata">

**Author:** ![Anirban\_K](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@Anirban\_K](https://community.deeplearning.ai/u/Anirban_K)\
**Post date:** [August 26, 2023, 3:35pm UTC](https://community.deeplearning.ai/t/invalid-base64-string-returned-from-rest-api/411491/4 "2023-08-26T15:35:04Z")

</div>

This is very helpful. I am also able to make it work now. Thanks.
