0

I am trying to get Python3 to PUT json info in string format to an API. And I want to do it without

import requests

Thus far I am stuck with this code:

import urllib.request
import urllib.parse
import json
url = "http://www.example.com"
DATA = json.dumps({'grades': {"math": "92", "chem": "39"}})
req = urllib.request.Request(url, data=DATA, method='PUT')
response = urllib.request.urlopen(req)

Naturally this raises the error:

raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

To get rid of the error I can do:

DATA= str.encode(DATA)

But this turns my data into bytes format, instead of string that I want to put up. Is there a way to PUT up strings without importing "requests"?(importing anything that comes with python install is OK). Or can I PUT up a *.json file?

Essentially I am trying to do the opposite of this.

Community
  • 1
  • 1
Ken
  • 1
  • 3

0 Answers0