When using the shell, I can successfully create a new user by running
curl --user administrator:pasword "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d #user.json
However when I try to do the same thing in python using requests, I get a 200 response and no user is created.
This is the script I am using:
import requests
headers = {
'Content-Type': 'application/json',
}
data = open('user.json')
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=data, auth=('Administrator', 'password'))
print(response)
To me they look the same. What is different in the python request that is stopping the user from being created?
I compare the post date with curl and python requests.And i found the difference.
CURL: {"user" : "hero","pd":30}
Requests: pd=30&user=hero
Then this is my test.
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('http://127.0.0.1:8080',
headers=headers,
json = data,
auth=('Administrator', 'password'))
print(response.headers)
I think using json = data would work as well, but I was finally successful using json dumps: data=json.dumps(data)
Related
I've have been accessing an supportpal API via curl just fine using the following command. (https://docs.supportpal.com/current/REST+API)
curl.exe -i -u 'APIKEY:x' -X GET https://support.url.org/api/user/user/3697
This correctly grabs the data. I've trying replicate this with python but i continually have issues with authentication and get the following error.
Failed to authenticate because of bad credentials or an invalid authorization header
The code i'm using is straight forward.
import requests
import json
url = "https://support.url.org/api/user/user/3697"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer: {APIKEY:x}"
}
response = requests.request("GET", url, headers=headers)
print(response. Text)
I'm thinking i have an issue with the auth header, but can't figure it out.
from requests.auth import HTTPBasicAuth
import requests
url = 'https://support.url.org/api/user/user/3697'
headers = {'Accept': 'application/json'}
auth = HTTPBasicAuth('apikey', x)
req = requests.get(url, headers=headers, auth=auth)
I have a curl command as follows that can make a restful API call to my webservice:
curl 'https://my-web-service/...' -H 'Connection: keep-alive' -H 'Cookie: csrftoken=xxx; sessionid=xxx'
Now how do I have such command ported in python3 code?
Here is what I have initial coded in python3, which is not working yet.
import requests
csrftoken = 'xxx'
sessionid = 'xxx'
api_url = "https://my-web-service/..."
header = {"Cookie": ? }
response = requests.get(api_url, headers=header)
print(response.status_code)
Because I don't know how to fit both sessionid and csrftoken value in the hearder part. Any ideas ?
Thanks,
Jack
headers can typically be passed as a dictionary via requests.
import requests
url = "https://my-web-service/..."
headers = {'Cookie': 'xxx', 'sessionid': 'xxx'}
response = requests.get(url, headers=headers)
I am trying to make API requests with proper authorization. I looked at Conversion of curl to python Requests but am having trouble putting the authorization headers.
This is what I have so far:
import base64
import http.client
import requests
import json
headers = {
"Content-Type": "application/json",
'Authorization': 'Basic %s' % base64.b64encode(b'username:password').decode('ascii'),
}
payload = {json.dumps({"one":["two:three:four"]})}
url = 'https://website/v1'
r = requests.post(url, data=payload, headers=headers)
if __name__=='__main__':
print (r.text)
The only difference between the link provided above and my code is that he has query={"tags":["test1","test2"]} I do not have that at all.
This is the curl I am trying to translate to get what I got above.
curl -X POST -u "username:password" -H "Content-Type:application/json" "https://website.com" -d '{"ids":["something:numberOne:numbertwo"]}'
Any help appreciated.
So here is my curl command, In command line, curl script is working fine
curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -u user:password URL -d '{"key1":"value1","key2":"value2"}'
Now I have converted my code to python and the code look like this :
import requests
import json
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
data = '{"key1":"value1","key2":"value2"}'
requests.post('URL', headers=headers, data=data, auth=('user', 'password'))
When I ran this code, I am not getting any output.
Please let me know what I am doing wrong in here.
At first, the 'url' parameter in requests.post should be an complete url including the 'http://' or 'https://'.
Secondly, if you need to post some data in form of application/json, using Python protogenic dict will be fine. like this
data = {"key1":"value1","key2":"value2"}
you might be looking at something like this?
r = requests.post("http://{}/Command?".format(web_addr), params=dict)
print r.text
In Python 3.6.1, you should get the response 200 message if the following code gets executed, with a existing 'URL':
from requests import post
data = {"key1":"value1","key2":"value2"}
print(post('URL', json=data))
I am writing a python script to using an API to pull down JSON data using requests.
A cleaned up snippet from the CURL that works is (with altered key and URL):
curl -G -H 'key: wwwxxxx' -H 'Content-Type: application/json' --data-urlencode 'from=2017-01-01 12:00:00' https://sampleurl/target1
How do I handle the "--data-urlencode 'from=2017-01-01 12:00:00'" ?
I think the code would be:
import requests
headers = {
'key': 'wwwxxxx',
'Content-Type': 'application/json',
}
url = 'https://sampleurl/target1'
data = requests.get(url, headers=headers)
Thanks in advance for any help!
UPDATE
I tried the DATA suggested by zwer, but that threw a 404 error.
They i just tried to add the parameter as a header pair and it worked!!!
So the code that works is:
import requests
headers = {
'key': 'wwwxxxx',
'Content-Type': 'application/json',
'from' : '2017-01-01 12:00:00'
}
url = 'https://sampleurl/target1'
data = requests.get(url, headers=headers)
Just use the data parameter:
import requests
headers = {
'key': 'wwwxxxx',
'Content-Type': 'application/json',
}
url = 'https://sampleurl/target1'
data = requests.get(url, headers=headers, data={"from": "2017-01-01 12:00:00"})