0

The first axios.get is getting data from local server and then pass into axios.post into 3rd party server.

If i pass 'data' (hardcoded) into axios.post, 3rd party server send back response as success operation, but if i use 'data1' from axios.get, they send back respond that something error/not valid in my data, which actually, hardcoded 'data' = 'data1' as a result axios.get.

Where is something wrong in my code?

const catalog = async (req, res) => {

const result = await axios.get('http://localhost:3500/catalog/SM-P6010ZKEXSE')
const data1 = JSON.stringify(result.data)

const data = 
{"Catalogues":[{"siteCode":"","modelCode":"SM-P6010ZKEXSE","modelDesc":"Tablet SM-601 BLACK XSE  E2014","datelist":[{"price":7450000,"status":"A","startDate":"20230509100316","endDate":"20240508100316"}]}]}

const header = {
    "headers": {
        "Authorization": "Bearer xyz",  
    }
    } 

try {
    const result = await axios
        .post('https://external-server/catalog', data , header)
    
    {
        console.log('Update catalog success')
        console.log('status : ' + result.status)
        console.log(result.data)
        res.json(result.data)
    }
} catch (error) {
    console.log('Failed to update catalog')
}
}

1 Answer 1

0

I can see from the above statement that you are using JSON.stringify. This will convert your object to string and your data variable is of type object change its type.

First, see your response and check if it is an object, then pass directly response data, which is an object in most cases. If it is not, then convert it to the appropriate type as your post request accepts.

if your response is already object, then change

const data1 = JSON.stringify(result.data) 

to

const data1 = result.data
Sign up to request clarification or add additional context in comments.

2 Comments

yes it's work, finally
@Dale , what do you mean about?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.