-1

A post request with axios get http error 500. This is the code:

async function getUserTokenByRefresh(refreshToken) {
    const encodedStr = base64Encode(`${process.env.EBAY_SANDBOX_APPID}:${process.env.EBAY_SANDBOX_CERTID}`);
    const auth = `Basic ${encodedStr}`;
    const options = {
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: auth
        }
    };
    const data = {
        grant_type: "refresh_token",
        refresh_token: refreshToken       
    };
    const testing = true;

    const url = testing
    ? "https://api.sandbox.ebay.com/identity/v1/oauth2/token"
    : "https://api.ebay.com/identity/v1/oauth2/token";
    
    try {
        const response = await axios.post(
            url,
            data,
            options
        );
        console.log(JSON.stringify(response));
    }
    catch (e) {
        console.log(JSON.stringify(e));
    }

}

This is the error message:

{
  "message": "Request failed with status code 500",
  "code": "ERR_BAD_RESPONSE",
  "status": 500
}

This is the error message in json format. I don't know what's wrong in the code. Can you check it?

blob
  • 439
  • 8
  • 21
  • The remote server responds with "500 Internal Server Error" - you're possibly sending it bad data. Without knowing what should be sent to that endpoint, we can't help. – AKX Jul 24 '22 at 19:00
  • Could you please add basic formatting to the error message so that it isn't just one long line without line breaks – derpirscher Jul 24 '22 at 19:00
  • Thank you, I've solved the data should be encoded: new URLSearchParams(data) – blob Jul 24 '22 at 19:21

1 Answers1

0

Data should be encoded.

async function getUserTokenByRefresh(refreshToken) {
    const encodedStr = base64Encode(`${process.env.EBAY_SANDBOX_APPID}:${process.env.EBAY_SANDBOX_CERTID}`);
    const auth = `Basic ${encodedStr}`;
    const options = {
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            Authorization: auth
        }
    };
    const data = {
        grant_type: "refresh_token",
        refresh_token: refreshToken       
    };
    const testing = true;

    const url = testing
    ? "https://api.sandbox.ebay.com/identity/v1/oauth2/token"
    : "https://api.ebay.com/identity/v1/oauth2/token";
    
    try {
        const response = await axios.post(
            url,
            //ENCODED DATA
            new URLSearchParams(data),
            options
        );
        console.log(JSON.stringify(response));
    }
    catch (e) {
        console.log(JSON.stringify(e));
    }

}
blob
  • 439
  • 8
  • 21