• LilFirecracker
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I have a HTTP Request that sends out an endpoint to an Azure function. I've added the endpoint url to Remote Site Settings. When I send the request in Postman, I get a 200 OK status. When I send this request from Salesforce, I get a 400 Bad Request. 

I have tried to set Content-Type and Accept to application/json but keep getting the 400 error and the response header as text/plain (see debug log). 

Why isn't the response header Content-Type set to application/json? Is there any other way to make sure the response is being returned as application/json? This is my first time with an http request so if I'm missing something or if you have any ideas on a fix, I would be super grateful! 
public static Boolean mergeTest(String masterId, String followerId){

       JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStartObject();      
        gen.writeStringField('is_sandbox', String.valueOf(Utils.runningInASandbox()));
        gen.writeStringField('master',masterId);
        gen.writeStringField('follower',followerId);
        gen.writeEndObject();    
        String jsonS = gen.getAsString();
        String endpoint = 'https://xxxxxxxxxxx.azurewebsites.net/api/FunctionName?code=defaultFunctionKey==';
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setbody(jsonS);
        req.setHeader('x-functions-key', 'masterHostKey');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Accept', 'application/json');
        Http http = new Http();
        req.setTimeout(120000);
        HTTPResponse response;
        if (!Test.isRunningTest()){
            try{
                response = http.send(req);
                if (response.getStatusCode() == 500) {
                    return False;
                }
            } catch (System.Exception e){
            	System.debug('Callout exception error: '+e);
            }      
        }       
        return True;
    }
Screen shot
I have a HTTP Request that sends out an endpoint to an Azure function. I've added the endpoint url to Remote Site Settings. When I send the request in Postman, I get a 200 OK status. When I send this request from Salesforce, I get a 400 Bad Request. 

I have tried to set Content-Type and Accept to application/json but keep getting the 400 error and the response header as text/plain (see debug log). 

Why isn't the response header Content-Type set to application/json? Is there any other way to make sure the response is being returned as application/json? This is my first time with an http request so if I'm missing something or if you have any ideas on a fix, I would be super grateful! 
public static Boolean mergeTest(String masterId, String followerId){

       JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStartObject();      
        gen.writeStringField('is_sandbox', String.valueOf(Utils.runningInASandbox()));
        gen.writeStringField('master',masterId);
        gen.writeStringField('follower',followerId);
        gen.writeEndObject();    
        String jsonS = gen.getAsString();
        String endpoint = 'https://xxxxxxxxxxx.azurewebsites.net/api/FunctionName?code=defaultFunctionKey==';
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('POST');
        req.setbody(jsonS);
        req.setHeader('x-functions-key', 'masterHostKey');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Accept', 'application/json');
        Http http = new Http();
        req.setTimeout(120000);
        HTTPResponse response;
        if (!Test.isRunningTest()){
            try{
                response = http.send(req);
                if (response.getStatusCode() == 500) {
                    return False;
                }
            } catch (System.Exception e){
            	System.debug('Callout exception error: '+e);
            }      
        }       
        return True;
    }
Screen shot