2

I'm try using Post method in axios to send some data, but the result of my code only undefined. This is my code to post http request using axios:

const Axios = use('axios');
const Env = use('Env');
const querystring = require('querystring');

class getTrackingData({ response }) {

    const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
            {
                data: querystring.stringify({
                    waybill : 'SOCAG00183235715', courier : 'jne'
                })
            },
            {
                headers: {
                    'key':Env.get('APP_KEY'),
                    'content-type': "application/x-www-form-urlencoded"
                }
            }).then(function(response) {
                console.log(response.data);
                //return response.data;
            });
        return tracking;
}
}

What's wrong with this code?

chiper4
  • 303
  • 5
  • 18

2 Answers2

1

Try with this

'content-type': "application/application.json"

For detail example

Update

Please add this in the header

headers: {
   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}

Refrence Link

Sachin Shah
  • 4,503
  • 3
  • 23
  • 50
  • 1
    the result is bad request – chiper4 Nov 13 '19 at 22:11
  • Yes, that means your request reaches to the server completely. Now just you need to check that which params require on the server-side. For example, Need some data in the header and in the body. For that, you can just send your body and header params with your API developer and check that what is wrong in req. After that, your API will work fine. – Sachin Shah Nov 14 '19 at 04:56
  • @isnainibarochatun Just try one more way. I have updated my answer. – Sachin Shah Nov 14 '19 at 05:07
0

I don't know but i think this is because the format data that sent is unreadable, so i'm tried to change the format of the data sent like this:

const waybill = 'SOCAG00183235715'
const courier = 'jne'

    const tracking = await Axios.post(Env.get('APP_ENDPOINT') + '/waybill',
                //{
                    //data: querystring.stringify({
                      //  waybill : 'SOCAG00183235715', courier : 'jne'
                    //})
                //},

                    //change code above to bellow:
                    'waybill='+waybill+'&courier='+courier,
                {
                    headers: {
                        'key':Env.get('APP_KEY'),
                        'content-type': "application/x-www-form-urlencoded"
                    }
                }).then(function(response) {
                    console.log(response.data);
                    //return response.data;
                });
            return tracking;

And this is work to me!

chiper4
  • 303
  • 5
  • 18
  • Hey, can you please try one thing with just pass the simple object. No need to stringify the object. Let me know if you didn't understand this. And also I shared the example in my answer. Have you check it and tried that way ? Because I'm also calling API that way also and it works fine for me. :) – Sachin Shah Nov 14 '19 at 04:59
  • i'm try add ```; charset=UTF-8``` in headers content-type but parameter data that will sent still same using this format: ```'waybill='+waybill+'&courier='+courier,``` it work. But if this format data sent using json object, the return is **bad request** – chiper4 Nov 14 '19 at 05:55