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
Nitin Rawat 9Nitin Rawat 9 

Bad Request and Status Code 400 error message

Hi, 

This is my code to upload File to s3 bucket.

public with sharing class AWS_File_Upload_Ctrl {

    public static String testIntegration(String filename, String base64Data , String contentType) {
        System.debug('filename---->>'+filename);
        System.debug('base64Data---->>'+base64Data);
        System.debug('contentType---->>'+contentType);
        Blob beforeblob = EncodingUtil.base64Decode(base64Data);
        //Blob beforeblob = Blob.valueOf(base64Data);
        
        String attachmentBody = EncodingUtil.base64Encode(beforeblob);
        String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
        String key = 'AKIAJNWEGTHJFMQQLPBA';
        String secret = '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';
        String bucketname = 'personalsfbucket';
        String foldername = 'DocumentUploaded';
        String host = 's3.ap-south-1.amazonaws.com';
        String method = 'PUT';

        HttpRequest req = new HttpRequest();
        req.setMethod(method);
        req.setEndpoint('https://' + bucketname + '.' + host + '/' + fileName); //('https://' + host + '/' + bucketname + '/' + filename);
        req.setTimeout(120000);
        req.setHeader('Host', bucketname + '.' + host);
        req.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
        req.setHeader('Content-Encoding', 'base64');
        req.setHeader('Content-type', contentType);
        req.setHeader('Connection', 'keep-alive');
        req.setHeader('Date', formattedDateString);
        req.setHeader('ACL', 'public-read');
        req.setBodyAsBlob(beforeblob);
        System.debug('req---->'+req);
        /* String stringToSign = 'PUT\n\n' +
                contentType + '\n' +
                formattedDateString + '\n' +
                '/' + bucketname + '/' + bucketname + '/' + filename; */

        String stringToSign = 'PUT\n' +
                contentType + '\n' +
                formattedDateString + '\n' +
                '/' + bucketname + '/' + bucketname + '/' + filename;
        System.debug('stringToSign---->>'+stringToSign);

        String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
        Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret));
        String signed = EncodingUtil.base64Encode(mac);
        String authHeader = 'AWS' + ' ' + key + ':' + signed;
        System.debug('authHeader---->>'+authHeader);
        req.setHeader('Authorization',authHeader);
        String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8');

        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('RESPONSE STRING: ' + res.toString());
        System.debug('RESPONSE STATUS: ' + res.getStatus());
        System.debug('STATUS_CODE: ' + res.getStatusCode());
        if(res.getStatusCode() == 200){
            String ReturnUrl = 'https://'+host+'/'+bucketname+'/'+bucketname+'/'+filename;
            System.debug('ReturnUrl---->>'+ReturnUrl);
            return ReturnUrl;
        }
        return 'ERROR';
    }
}

I am getting error message while runing it through Anonymous Window:

System.HttpResponse[Status=Bad Request, StatusCode=400]

Please Help:(

 

Thanks in advance:)

AbhishekAbhishek (Salesforce Developers) 
Your query is answered here,

https://salesforceoke.blogspot.com/2019/02/status-code-400-bad-request-in.html


For further reference check this too,

https://salesforce.stackexchange.com/questions/123416/status-bad-request-statuscode-400-on-rest-api-call


If it helps you and closes your query by marking it as solved so that it can help others in the future.

Thanks.
gargvarun gargvarungargvarun gargvarun

Thanks for the clarification.