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
LilFirecrackerLilFirecracker 

Http Response defaults Content-Type to text/plain, not application/json

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
Abdul KhatriAbdul Khatri
Can you please share you postman screen shot where you are getting 200?
LilFirecrackerLilFirecracker
Below are screenshots using the same params, header and body in postman where the status is 200. I've also included the curl. 

User-added image
User-added image
User-added image
User-added image

 One other thing I noticed in postman...the response headers tab shows the same as the debug logs in Salesforce. Could the 400 error be related to something else? Where/how can I get the curl from Salesforce? 

User-added image


 
Abdul KhatriAbdul Khatri
Can you check what value you are getting in is_Sandbox, as per postman it should be string True or False?

Also remove the accept part from the Header?

Just for testing, try to hard code the values and check how you get it.



 
LilFirecrackerLilFirecracker
Great news! I hard-coded is_Sandbox and the master and follower Ids and got a Success 200 OK. Thank you for all of your help in narrowing down where that issue was. 

I did run into 'System CalloutException: Read timed out' when using a larger data set. Since I already setTimeout to the max 120000, is the only other option to avoid this error to use a smaller data set or would you have any other suggestion to try?
Abdul KhatriAbdul Khatri
Glad to hear that. So it seems like the data in is_Sandbox was not passing as True/False instead it was getting pass 1/0 which might be causing the issue. 

Regarding Timeout setting, you are ok with that.

Would you be kind to mark my above comments as best if that helped narrow down your issue for others?