system.net.httpPost() throwing error while Postman is not

Hi Kevin,

Thanks for the quick response… That ended up being the issue.

Quick fix:

  1. Add import statement from urllib import urlencode.
  2. Call function urlencode() on body parameter.

Full function:

def getAccessToken():
	from urllib import urlencode
	headers = {"Content-Type": "application/x-www-form-urlencoded","Accept": "*/*","Accept-Encoding": "gzip, deflate, br","Connection": "keep-alive"}
	
	body = {"grant_type": "refresh_token","refresh_token": getRefreshToken()}
	
	contentType = "application/x-www-form-urlencoded"
	
	url = getWebServicesRootUrl() + '/services/rest/auth/oauth2/v1/token'

	res = system.net.httpPost(url, 
				contentType = contentType, 
				postData = urlencode(body), 
				username = getClientId(), 
				password = getClientSecret(), 
				headerValues = headers, 
				throwOnError = False
			)
	return res
1 Like