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
surya7628@gmail.com Ksurya7628@gmail.com K 

I am getting the error like 415 while using the rest api post method for update the record in mongo db from salesforce

Hi All,
I need to update the record in mongo db,hear i have the mongo db record id in salesforce, based on that id i need to update the status from
(in process to completed) when ever user changed the status from 'in process' to 'completed' in salesforce.

I am getting the error like (DEBUG|ResponceSystem.HttpResponse[Status=Unsupported Media Type, StatusCode=415]),please suggest me.

i have used below code:

public class AuthCallout {
    String payLoad = 'slug={!order.Mongo_DB_Order_ID__c}&OrderStatus={!order.Staus}';

    public void basicAuthCallout(){
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.mongolab.com/api/1/databases/itsstest/collections/testorders/{!order.Mongo_DB_Order_ID__c}?apiKey=K6PuNd6v3s8PKcUk1nby7tQQ9viniTkW');
        req.setMethod('GET');
        
        req.setMethod('PUT');
        req.setBody(payLoad); 
        HttpResponse res1 = http.send(req);
        System.debug('Responce'+res1);
   }
}
 
Prafull G.Prafull G.
You should set the appropriate content type or Accept type with request as per the mongolab api.

Try setting like this
req.setHeader('Content-Type', <value of content type supported by api i.e. application/json>);
OR
req.setHeader('Accept', <value of accept type supported by api i.e. application/json>);

let us know if it helps.
surya7628@gmail.com Ksurya7628@gmail.com K
Hi Prafull,

I have tried the below code,i am getting the error like System.HttpResponse[Status=Unsupported Media Type, StatusCode=415],hear i am getting the responce in get method,now i am facing the issue with put method.can u suggest me

global class AuthCallout {
    @future(callout=true)
    static webservice void basicAuthCallout(id ordid,string status){
        order ord = [select id,status,Mongo_DB_Order_ID__c from order where id=:ordid limit 1];
    
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.mongolab.com/api/1/databases/itsstest/collections/testorders/'+ord.Mongo_DB_Order_ID__c+'?apiKey=K6PuNd6v3s8PKcUk1nby7tQQ9viniTkW');
        req.setMethod('GET');
        HTTPResponse res = http.send(req);
        fromJSON obj =new fromJSON();
        obj=(fromJSON)JSON.deserialize(string.valueof(res.getbody()),fromJSON.class);
        system.debug('--------------------'+obj.OrderStatus);
        
        obj.OrderStatus =status;
        HttpRequest putReq = new HttpRequest();
        putReq.setEndpoint('https://api.mongolab.com/api/1/databases/itsstest/collections/testorders/'+ord.Mongo_DB_Order_ID__c+'?apiKey=K6PuNd6v3s8PKcUk1nby7tQQ9viniTkW');
        putreq.setMethod('PUT');
        system.debug('-------------------'+JSON.serialize(obj));
        putreq.setBody(JSON.serialize(obj));
        HTTPResponse putRes = http.send(putReq);
        system.debug('-----------------'+putRes );  
    }   
}
-------------------------------------------------

Below one is the record in mongo db :

{
    "_id": {
        "$oid": "56bd84d211576df406cce9bd"
    },
    "slug": "56bd84d211576df406cce9bd",
    "SFID": "",
    "ITCSV": "",
    "ITReport": "",
    "NTCSV": "",
    "NTReport": "",
    "OrderStatus": "Completed",
    "OrderDate": {
        "$date": "2016-02-12T07:08:01.000Z"
    },
    "ContactNumber": "987456321",
    "email": "test@test.com",
    "LenderBranchCode": "test",
    "OrderNumber": "new value",
    "LoanApplicantName": "test",
    "LoanRefNum": "Test3",
    "__v": 0
}

---------------->i have converted above record from json to apex

public class fromJSON{
    //public cls__id _id;  //when i try to save this class it is not allow to save for that i have changed this to [public clsid id] 
    public clsid id ;i am using this one insted of above public cls__id _id;
    public String slug;    //56bd84d211576df406cce9bd
    public String SFID;    //
    public String ITCSV;    //
    public String ITReport;    //
    public String NTCSV;    //
    public String NTReport;    //
    public String OrderStatus;    //Completed
    public cls_OrderDate OrderDate;
    public String ContactNumber;    //987456321
    public String email;    //test@test.com
    public String LenderBranchCode;    //test
    public String OrderNumber;    //new value
    public String LoanApplicantName;    //test
    public String LoanRefNum;    //Test3
    public Integer v;    //0
    //class cls__id {
        //public String $oid;    //56bd84d211576df406cce9bd
   // }
    class clsid {
        public String oid;    //56bd84d211576df406cce9bd
    }
    class cls_OrderDate {
        public String $date;    //2016-02-12T07:08:01.000Z
    }
    public static fromJSON parse(String json){
        return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
    }
}



-------------------------------------------------

And i am calling this class in after update trigger.

if(trigger.isupdate)
    {
    AuthCallout.basicAuthCallout(trigger.new[0].id,trigger.new[0].status);    
    }

 
Prafull G.Prafull G.
For PUT method also, you have to specify accept or content type similar to GET.
Please try!