+ Start a Discussion
Diwakar G 7Diwakar G 7 

Conversion from Apex to Python

Hi, 

Please help me to convert the below code to Python. 
var xhttp = new XMLHttpRequest(); 
xhttp.open("POST", "http://instance.salesforce.com/services/data/v44.0/tooling/runTestsSynchronous/", true) 
xhttp.setRequestHeader("Authorization", "OAuth <SESSION_ID>") 
xhttp.setRequestHeader('Accept',"application/json"); 
testObject = {tests: [{classId: "N0tARealClassId", testMethods: ["testMethod1", "testMethod2"]}]} 
requestObject = json.stringify(testObject); 
response = xhttp.send(requestObject) 
response = JSON.parse(response)

This code is used to run Apex test. Please help me.

Thanks and Regards,
Diwakar G
SandhyaSandhya (Salesforce Developers) 
Hi,

You can try below sample code
import urllib2
from urllib2 import Request, urlopen
import json
values =[{"className":"TestVerifyDate","testMethods": ["testMethod1","testMethod2","testMethod3"]}]
values = json.dumps(values).encode('utf8') 
your_url="http://instance.salesforce.com/services/data/v44.0/tooling/"
req = Request(your_url, data=values,headers = {'Accept':'application/vnd.rn+json','Content-Type':'application/vnd.rn+json','Authorization':'Token token="<hexstring>"'})
response_body = urlopen(req).read()
print response_body

Refe​​​​​​rence​ link.

https://www.reddit.com/r/learnpython/comments/5a8fi9/sending_authorization_token_header_through_python/
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 
Diwakar G 7Diwakar G 7
Hi,

What I need to give in token?

I tried security token, API key but it is not working. I am getting 401 unauthorized error.

Please help me.

Thanks and Regards,
Diwakar G