Search code examples
pythonarraysjsonipfs

Error while uploading file on IPFS (TypeError: expected string or bytes-like object)


I am trying to upload a file on IPFS and retrieve it. The tutorial I am following uses the following approach:

import requests
import json

files = {
    "file" : ("Congrats! You have uploaded this on IPFS."),
}

response_hash = requests.post("https://ipfs.infura.io:5001/api/v0/add", files = files)
p = response_hash.json()
hashed = p["Hash"]
print(p)
print(hashed)

params = (
    ("arg", hashed),
    )
response = requests.post("https://ipfs.infura.io:5001/api/v0/block/get", params = params)
print(response.text)

However, I want to upload multiple data, preferably in the form of json arrays. I tried to modify it but I'm running into an error.

My code:

import requests
import json

example = {

"employees":[  
    {"name":"Shyam", "email":"[email protected]"},  
    {"name":"Bob", "email":"[email protected]"},  
    {"name":"Jai", "email":"[email protected]"}  
]}

response_hash = requests.post("https://ipfs.infura.io:5001/api/v0/add", files = example)
p = response_hash.json()
hashed = p["Hash"]
print(p)
print(hashed)

params = (
    ("arg", hashed),
    )
response = requests.post("https://ipfs.infura.io:5001/api/v0/block/get", params = params)
print(response.text)

Error:

Traceback (most recent call last):
  File "ipfs_v1.py", line 16, in <module>
    response_hash = requests.post("https://ipfs.infura.io:5001/api/v0/add", files = example)
  File "E:\Anaconda3\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "E:\Anaconda3\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "E:\Anaconda3\lib\site-packages\requests\sessions.py", line 516, in request
    prep = self.prepare_request(req)
  File "E:\Anaconda3\lib\site-packages\requests\sessions.py", line 459, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "E:\Anaconda3\lib\site-packages\requests\models.py", line 317, in prepare
    self.prepare_body(data, files, json)
  File "E:\Anaconda3\lib\site-packages\requests\models.py", line 505, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "E:\Anaconda3\lib\site-packages\requests\models.py", line 166, in _encode_files
    rf.make_multipart(content_type=ft)
  File "E:\Anaconda3\lib\site-packages\urllib3\fields.py", line 268, in make_multipart
    ((u"name", self._name), (u"filename", self._filename))
  File "E:\Anaconda3\lib\site-packages\urllib3\fields.py", line 225, in _render_parts
    parts.append(self._render_part(name, value))
  File "E:\Anaconda3\lib\site-packages\urllib3\fields.py", line 205, in _render_part
    return self.header_formatter(name, value)
  File "E:\Anaconda3\lib\site-packages\urllib3\fields.py", line 116, in format_header_param_html5
    value = _replace_multiple(value, _HTML5_REPLACEMENTS)
  File "E:\Anaconda3\lib\site-packages\urllib3\fields.py", line 89, in _replace_multiple
    result = pattern.sub(replacer, value)
TypeError: expected string or bytes-like object

What am I doing wrong? How do I upload json arrays onto IPFS?


Solution

  • converted employee details as string

    import requests
    import json
    
    files = {
        "employees" : ( """{"name":"Shyam", "email":"[email protected]"},  
        {"name":"Bob", "email":"[email protected]"},  
        {"name":"Jai", "email":"[email protected]"}  """),
    }
    
    response_hash = requests.post("https://ipfs.infura.io:5001/api/v0/add", files = files)
    p = response_hash.json()
    hashed = p["Hash"]
    print(p)
    print(hashed)
    
    params = (
        ("arg", hashed),
        )
    response = requests.post("https://ipfs.infura.io:5001/api/v0/block/get", params = params)
    print(response.text)
    

    output:

    {'Name': 'employees', 'Hash': 'QmeGTapzFr36Bag6c1w4ZxiuJVM8wxDGMD7GFFmc7onV8c', 'Size': '161'}
    QmeGTapzFr36Bag6c1w4ZxiuJVM8wxDGMD7GFFmc7onV8c
    
    ╗ {"name":"Shyam", "email":"[email protected]"},
    {"name":"Bob", "email":"[email protected]"},
    {"name":"Jai", "email":"[email protected]"}