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 

Getting error message of Status=Bad Request, Status Code=400

Hi,

I am getting "status code is 200" pass when I test it through POSTMAN. I am also able to see my uploaded file on my s3 bucket folder. This is the endpoint I am using "https://personalsfbucket.s3.ap-south-1.amazonaws.com/DocumentUploaded/Demo". Now when I use the same endpoint in my Code : 
req.setEndpoint('https://' + bucketname + '.' + host + '/' + foldername + '/' + fileName);

I am getting "System.HttpResponse[Status=Bad Request, StatusCode=400]".

Any Help or suggestion!!!!

For your reference here's my code: 

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 + '/' + foldername + '/' + 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';
    }
}

What I am trying to do is uploading file to s3 bucket of AWS directly without saving it in SF.

Please do let me know what/where I am doing wrong and provide any helpful links.

Thanks in advance!!

ShirishaShirisha (Salesforce Developers) 
Hi Nitin,

Greetings!

Please double check the credentials along with the body which you are trying to pass as it is case sensitive.Please refer the below for troubleshooting the issue.

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

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri