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
Test Dev 81Test Dev 81 

How to fix 422 Unprocessable Entity in httpcallout

hi everyone,
I have created an apex class to make some HTTP callout but when I send the request I am getting error in response
"422 Unprocessable Entity" even though I have put request.setHeader('Content-Type', 'application/json); in request header
 
public class Rg_TestMemberAccountCreate {
    public static void testMemberAccount(String endpoint,String token){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(endpoint);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('Authorization', 'Bearer '+token);
        request.setHeader('Accept', 'application/json');
        // Set the body as a JSON object
        String b = '{"data": {"attributes": {"name": "Test Rahul2","symbol": "TESTRT", "is_active": "true"},"type": "providers"}}';
        request.setBody(b);
        System.debug('Requst : '+request);
        HttpResponse response = http.send(request);
        System.debug('response: '+response);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }   
    }
}
do you have any idea what is wrong in my class?
Thank You,
Rahul

 
Best Answer chosen by Test Dev 81
Test Dev 81Test Dev 81
Found the issue after trying 5 hrs,
there are some required fields that need to be sent in body  after adding that issus is solved
Thanks, Swetha ,now its working

All Answers

SwethaSwetha (Salesforce Developers) 
HI Rahul,
Can you provide the endpoint so I can test this code from my developer console's anonymous block? Thanks

Related:https://stackoverflow.com/questions/16133923/400-vs-422-response-to-post-of-data
https://stackoverflow.com/questions/56711503/how-to-fix-422-unprocessable-entity-when-sending-a-post-request-to-redmine-api
Test Dev 81Test Dev 81
Found the issue after trying 5 hrs,
there are some required fields that need to be sent in body  after adding that issus is solved
Thanks, Swetha ,now its working
This was selected as the best answer