0

I want to get a response from post request to a site that requires password and json data as input. The code looks like this:

import requests
from requests.auth import HTTPBasicAuth
import json


url = 'http://ssss.ct.xxx:9000/xxx.xx_xxx/xxxx/get/json/'
data = '''{"RowSelectors":[{"Name":"xx","Value":"100649","Operation":"OR" },
{"Name":"xx","Value":"577","Operation":"OR" }],"TableName":"YY"}'''

response = requests.post(url, data=json.loads(data), auth = HTTPBasicAuth('my_user', 'my_password'))
print(response)

And the print function shows me a "Response [415]", which is "Unsupported Media Type" error. I expect it to return me a result from the query in variable "data". I know that something might be wrong with this data, but I don't know how to solve this.

I also tried using another library:

import urllib.request
import urllib.parse
import json
data1 = '''{"RowSelectors":[{"Name":"xx","Value":"100649","Operation":"OR" },
{"Name":"xx","Value":"577","Operation":"OR" }],"TableName":"YY"}'''

data1= json.loads(data1)
params = urllib.parse.urlencode(data1)
x = urllib.request.Request('http://ssss.ct.xxx:9000/xxx.xx_xxx/xxxx/get/json/', data=params)
y= urllib.request.urlopen(x, data = params)

print(y.read())

But it doesn't work too. The error in this case is: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. And I don't understand the documentation on how i should include the user and password to this code.

Additional maybe useful info : I'm using soapUI and I have 3 inputs that I want to have. First is endpoint=http://ssss.ct.xxx:9000, then its resource=/xxx.xx_xxx/xxxx/get/json/ and the SQL query written in data. I expect my response to be a table in json.

I appreciate any help;)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jack123321
  • 57
  • 2
  • 11
  • do you have a request that works? curl or web or something? – shahaf Feb 13 '18 at 12:13
  • This request works directly using soapUI. But I try to call this request from python ;) I'm just trying to make it work – Jack123321 Feb 13 '18 at 12:16
  • You are posting the JSON data as a Python dictionary, effectively posting *form data*, not JSON. *Don't decode the JSON string*, you need to post it as is. – Martijn Pieters Feb 13 '18 at 12:41
  • Alternatively, use the `json=` argument, so `requests.post(url, json=json.loads(data), auth=HTTPBasicAuth('my_user', 'my_password'))`. – Martijn Pieters Feb 13 '18 at 12:42
  • I'm not sure why you encoded your data to form encoding in your `urllib` attempt either. You said that are posting to something that takes *json data as input*, so post JSON, not form data. – Martijn Pieters Feb 13 '18 at 12:44
  • Doing it like that gives me response : The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. – Jack123321 Feb 13 '18 at 12:47
  • @Jack13321: then consult your documentation and see what kind of payload format the server *does* accept. Know that using `data=json_string` will not set any `Content-Type` header, so that could be an issue. `json=python_structure` will set `Content-Type: application/json`. – Martijn Pieters Feb 13 '18 at 13:22

0 Answers0