2

I need help in post request in lwc. Also can you help me to post image with rest api, Below is get request

fetch('https://skpycloud.free.beeceptor.com', // End point URL
        {
            // Request type
            method: "GET",
            'body': JSON.stringify({
                //         'Name': 'Salesforce',
                //         'BillingStreet': '1 Market Street',
                //         'BillingCity': 'San Francisco',
                //         'BillingState': 'CA'
            }),
            headers: {
                // content type
                "Content-Type": "application/json",
                // adding your access token 
                "Authorization": "OAuth 00DB0000000EfVQ!AQwAQEiiynMU2EsBcS2PhXSQ6KQTTG.Zr0hlDHTFcGcAPqKQOBNDB0rwyASZK44fqIAVe6GrVNZPsAWJ6iqXLNBfSQ.dqvW1",
            }
        })
        .then((response) => {
            return response.json(); // returning the response in the form of JSON
        })
        .then((jsonResponse) => {

            let objData = {
                From_Currency_Name: '',
                From_Currency_Code: '',
                To_Currency_Name: '',
                To_Currency_Code: '',
                Exchange_Rate: '',
                Last_Refersed: '',
            };

            window.console.log('jsonResponse ===> ' + JSON.stringify(jsonResponse));
            // retriving the response data
            let exchangeData = jsonResponse['Realtime Currency Exchange Rate'];


        })
        .catch(error => {
            window.console.log('callout error ===> ' + JSON.stringify(error));
        })
2
  • Where are you getting the image from? What have you tried so far?
    – sfdcfox
    May 1, 2020 at 12:20
  • i get image from lightning-input (html) and changed it to base64 , now i need to post to rest api May 1, 2020 at 16:50

1 Answer 1

3

You should be able to do something like this:

fetch(
    endpoint,
    {
        method: "POST",
        body: fileBody,
        credentials: "include",
        headers: {
            contentType: fileContentType
        }
    }
)

If your credentials are invalid, then you can use a valid session ID as demonstrated in your original question. Make sure you use the proper values for fileBody and fileContentType. You should ideally not use a Base64 string, but instead the original binary data.

9
  • fetch( 'skpycloud.free.beeceptor.com', { method: "POST", body: encodeURIComponent(this.fileContents), //credentials: "include", headers: { contentType: "application/json", } } ) May 1, 2020 at 17:18
  • can you please check , i added encodeURI in body and what is the contenttype May 1, 2020 at 17:19
  • @RijwanMohammed That won't work. The body doesn't go in the URI, and you're not using application/x-www-form-urlencoded. The content type must match the file contents. If you're having trouble with a specific API, documentation on that API would be useful.
    – sfdcfox
    May 1, 2020 at 17:27
  • is there any limit in LWC to upload file ? May 1, 2020 at 17:53
  • @RijwanMohammed The only limits are whatever the server limits are.
    – sfdcfox
    May 1, 2020 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.