Reputation: 11
I am trying to create a google storage bucket using the GCP API's. I get HTTP Error 400:Bad Request error. Not sure where I am going wrong. But the curl command works.
Platform
Python 3.4.3/Ubuntu 14.04.5 LTS
Program:
import urllib.request import urllib.parse import ssl import http.client auth = auth="Bearer XXXXXXXXXXXXX"
def create_bkt(prj):
quoted_prj = urllib.parse.quote(prj, safe='')
url = 'https://storage.googleapis.com/storage/v1/b?project=%s' % (quoted_prj)
headers = {"Authorization": auth,
"Accept": "application/json",
"Content-Type": "application/json"}
context = ssl._create_unverified_context()
values = {"name":"bkt1"}
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req, context=context)
if __name__=="__main__":
call1('prj1')
send: b'POST /storage/v1/b?project=file-analyzer HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Type: application/json\r\nConnection: close\r\nAccept: application/json\r\nContent-Length: 12\r\nUser-Agent: Python-urllib/3.4\r\nAuthorization: Bearer ya29.A0AfH6SMCbKaWo9g9oo7JAvWtvvuPhfz2EPYU4PI8btAETtsNkS7vpvdeXHkFCb0UX93AzrQIbDKVcxyCdpCNh0DEtYi3cNxM2iZjhTT9QtKlqsVHZoq5xqQJ_mfnCA-VQeTNqpmggWQKe-pPD3HUL1mHV2mpg9ZiLegPji1K4Y30\r\nHost: storage.googleapis.com\r\n\r\nname=uk-bkt9'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: X-GUploader-UploadID header: Content-Type header: Date header: Vary header: Vary header: Cache-Control header: Expires header: Pragma header: Content-Length header: Server header: Alt-Svc header: Connection Traceback (most recent call last):
File "gcs-create-bkt.py", line 52, in <module>
call1('file-analyzer')
File "gcs-create-bkt.py", line 44, in call1
response = urllib.request.urlopen(req, context=context)
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
curl (works without any issue):
curl -v --request POST 'https://storage.googleapis.com/storage/v1/b?project=prj1' --header 'Authorization: Bearer XXXXXXXXXXXXX' --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{"name":"bkt1"}' --compressed
{
"kind": "storage#bucket",
"selfLink": "https://www.googleapis.com/storage/v1/b/bkt1",
"id": "bkt1",
"name": "bkt1",
"projectNumber": "676620708324",
"metageneration": "1",
"location": "US",
"storageClass": "STANDARD",
"etag": "CAE=",
"timeCreated": "2020-11-19T18:55:24.287Z",
"updated": "2020-11-19T18:55:24.287Z",
"iamConfiguration": {
"bucketPolicyOnly": {
"enabled": false
},
"uniformBucketLevelAccess": {
"enabled": false
}
},
"locationType": "multi-region"
}
Upvotes: 1
Views: 1292
Reputation: 75705
Your value is a dict and not a JSON.
Try this
import json
def create_bkt(prj):
quoted_prj = urllib.parse.quote(prj, safe='')
url = 'https://storage.googleapis.com/storage/v1/b?project=%s' % (quoted_prj)
headers = {"Authorization": auth,
"Accept": "application/json",
"Content-Type": "application/json"}
context = ssl._create_unverified_context()
values = {"name":"bkt1"}
data = urllib.parse.urlencode(json.dumps(values)).encode("utf-8")
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req, context=context)
...
Upvotes: 2