Sending JSON from Postman to Salesforce ([{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}])

171 views Asked by At

I'm trying to send a JSON string from Postman to Salesforce But I get this Error message [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}] with a 401 Unauthorized Status code

I've created a token successfully by creating a collection and then using OAuth2.0 to generate the token ( Password Credentials ) After that, I created a Post request under that Collection and used the same Token I got but I still same error.

here is the Object I want to Post { "FirstName": "your_value_here", "LastName": "your_value_here", "Email": "your_value_here", "MailingCity": "your_value_here", "MailingStreet": "your_value_here", "MailingPostalCode": "your_value_here", "House_Number__c": "your_value_here", "CRM__c": "your_value_here", "Salutation": "your_value_here", "GenderIdentity": "your_value_here", "Phone": "your_value_here" }

And here is the Apex Class That I created The Class parses the JSON file and creates a new contact And then returns the ID of the created contact

`@RestResource(urlMapping='/createdRelationWebhook') global with sharing class relationCreatedWebhook {

@HttpPost
global static void doPost() {
    RestResponse response = RestContext.response;
    response.addHeader('Content-Type', 'application/json');

    String requestBody = RestContext.request.requestBody.toString();
    System.debug(System.LoggingLevel.DEBUG, 'Received JSON: \n' + requestBody);

    Map<String, Object> relationValues = (Map<String, Object>) JSON.deserializeUntyped(requestBody);

    // Extracting required fields from the JSON
    String firstName = (String) relationValues.get('Firsname');
    String lastName = (String) relationValues.get('Lastname');
    String Phone = (String) relationValues.get('Phone');
    String email = (String) relationValues.get('Email');
    String city = (String) relationValues.get('City');
    String street = (String) relationValues.get('Street');
    String houseNumber = (String) relationValues.get('HouseNumber');
    String postcode = (String) relationValues.get('Postcode');
    String Prefix = (String) relationValues.get('Prefix');
    String CRM = (String) relationValues.get('CRM');
    String Gender = (String) relationValues.get('Gender');

   
    // Create a new Contact record
    Contact newContact = new Contact();
    newContact.FirstName = firstName;
    newContact.LastName = lastName;
    newContact.Phone = Phone;
    newContact.Email = email;
    newContact.MailingCity = city;
    newContact.MailingStreet = street;
    newContact.MailingPostalCode = postcode;
    newContact.House_Number__c = houseNumber;
    newContact.CRM__c = CRM; 
    newContact.Salutation = Prefix;
    newContact.GenderIdentity = Gender; 


    // Insert the new Contact record
    try {
        insert newContact;

        // Send response with the ID of the newly created Contact
        Map<String, String> jsonResponse = new Map<String, String>();
        jsonResponse.put('message', 'Contact created successfully');
        jsonResponse.put('contactId', newContact.Id);
        response.responseBody = Blob.valueOf(JSON.serialize(jsonResponse));
    } catch (Exception e) {
        // Handle any exceptions or errors
        Map<String, String> errorResponse = new Map<String, String>();
        errorResponse.put('error', 'Error creating contact: ' + e.getMessage());
        response.responseBody = Blob.valueOf(JSON.serialize(errorResponse));
    }
}

}`

The URL is https://-dev-ed.develop.lightning.force.com/services/apexrest/createdRelationWebhook

Hope you guys help me with this issue Thanks

1

There are 1 answers

0
eyescream On

The endpoint for your 2nd call is wrong. "Lightning" domain is just for UI access. You should take 2 things from successful login response - access token (also known as session id) and instance URL - the endpoint to use from now on.

You could hardcodenthe endpoint fro the calls but then it's crap to remember to change the endpoint as you move your integration from dev to UAT to prod... (Or when occasionally SF migrates your instance to another data centre or hyperforce). It's bit more work but read the endpoint and use it.

Ah, and make sure your user has the rights to access this class in profile/permission sets