function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex MakkAlex Makk 

How do I make a proper HTTP request?

Hello, I'm trying to follow API instructions and I'm stuck at the very beginning. Here is what it says:

Following the example, POST the username and password fields with your email and password, along with the grant_type value of password, to the https://api.safetyculture.io/auth endpoint.
 
curl \
  -X POST \
  --data-urlencode username=example@safetyculture.io \
  --data-urlencode password=your_password \
  -d grant_type=password \
  "https://api.safetyculture.io/auth"
Here is what I do:
String username = 'some.email@domain.com';
String password = 'apple';

HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.safetyculture.io/auth');
req.setMethod('POST');
req.setbody('username='+username+'&password='+password+'&grant_type=password');

Http http = new Http();
HTTPResponse response = http.send(req);

In debug logs I get:
13:17:54:754 CALLOUT_RESPONSE [15]|System.HttpResponse[Status=Bad Request, StatusCode=400]

Please advise. Thank you!
 
SwethaSwetha (Salesforce Developers) 
HI Alex,
Have you checked the relevant posts https://salesforce.stackexchange.com/questions/163256/rest-api-to-authenticate-with-basic-username-and-password
https://salesforce.stackexchange.com/questions/251911/which-grant-type-to-use-when-third-party-is-accessing-salesforce

Thanks