Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to get the same response when using fetch method #552

Closed
amrsa1 opened this issue Feb 17, 2020 · 18 comments
Closed

unable to get the same response when using fetch method #552

amrsa1 opened this issue Feb 17, 2020 · 18 comments

Comments

@amrsa1
Copy link

amrsa1 commented Feb 17, 2020

im tryin to test an API that use get method and also contains additional params in request headers (query string params)

image
image

    fit('3-  API will return status code 200', ()=> {
    return frisby
        .fetch('https://example.com/sss/ssss/ssss/207', {
            method: 'GET',
            request:{
                headers:
                    qs.stringify({
                        'additionalParams[include][0]': 'Platforms',
                        'additionalParams[include][1]': 'Platforms.PlatformType',
                        'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
                        ' additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
                        ' additionalParams[include][4]': 'Country',
                        'additionalParams[include][5]': 'State'
                    })
                }
        })
        .inspectJSON()

but im getting only this response

image

instead of this response that im getting from network in browser as shown below

image

@amrsa1
Copy link
Author

amrsa1 commented Feb 17, 2020

@vlucas @H1Gdev

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 17, 2020

@amrkamel1

Query params are not HTTP header.

Set query params as follows.

const url = 'https://example.com/sss/ssss/ssss/207'
const params = new URLSearchParams({ name: value })

.fetch(url + '?' + params {
method: 'GET',
})

@amrsa1
Copy link
Author

amrsa1 commented Feb 17, 2020

@H1Gdev

i have tried it getting the same response

 fit('6- Verify by adding aaa to last created account API will return status code 200', ()=> {
    const url = 'https://sss.ssss.com/ssss/sssss/companies/207';
    const params = new URLSearchParams({
        'additionalParams[include][0]': 'Platforms',
        'additionalParams[include][1]': 'Platforms.PlatformType',
        'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
        ' additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
        ' additionalParams[include][4]': 'Country',
        'additionalParams[include][5]': 'State' });
    return frisby
        .fetch(url + '?' + qs.stringify(params) , {
             method: 'GET'
        })
        .inspectJSON()

if i removed qs.stringfy from params

i get this response

 JSON: {
    "hasErrors": true,
    "resultList": [
        {
            "message": "\" additionalParams%5Binclude%5D%5B3%5D\" is not allowed",
            "key": [
                " additionalParams%5Binclude%5D%5B3%5D"
            ]
        },
        {
            "message": "\" additionalParams%5Binclude%5D%5B4%5D\" is not allowed",
            "key": [
                " additionalParams%5Binclude%5D%5B4%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B0%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B0%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B1%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B1%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B2%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B2%5D"
            ]
        },
        {
            "message": "\"additionalParams%5Binclude%5D%5B5%5D\" is not allowed",
            "key": [
                "additionalParams%5Binclude%5D%5B5%5D"
            ]
        }
    ]
}

@amrsa1 amrsa1 closed this as completed Feb 17, 2020
@amrsa1 amrsa1 reopened this Feb 17, 2020
@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 17, 2020

I think this can generate same URL.

const url = new URL('https://example.com/sss/ssss/ssss/207');
url.searchParams.append('additionalParams[include][0]', 'Platforms');
url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
url.searchParams.append('additionalParams[include][4]', 'Country');
url.searchParams.append('additionalParams[include][5]', 'State');

console.log(url.href);
/*
https://example.com/sss/ssss/ssss/207?additionalParams%5Binclude%5D%5B0%5D=Platforms&additionalParams%5Binclude%5D%5B1%5D=Platforms.PlatformType&additionalParams%5Binclude%5D%5B2%5D=Platforms.PlatformMobileOperator&additionalParams%5Binclude%5D%5B3%5D=Platforms.PlatformSatelliteOperator&additionalParams%5Binclude%5D%5B4%5D=Country&additionalParams%5Binclude%5D%5B5%5D=State
*/

@amrsa1
Copy link
Author

amrsa1 commented Feb 18, 2020

@H1Gdev

still same however console.log(url.herf) it prints the exact request url that i see in browser network tab
i even tried it with get method and still same error

fit('6- Verify by adding aaa to last created account API will return status code 200', ()=> {

        const url = new URL('https://test.com/companies/207');
        url.searchParams.append('additionalParams[include][0]', 'Platforms');
        url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
        url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
        url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
        url.searchParams.append('additionalParams[include][4]', 'Country');
        url.searchParams.append('additionalParams[include][5]', 'State');

        console.log(url.href);

        // const url = 'https://cisiot.test.att.com/mcapi/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State' });
        return frisby
            .fetch(url.href,{
                method: 'GET'
            })
            .inspectJSON() 
})

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 18, 2020

Query params look correct.
There seems to be another cause of issue.(HTTP header ??)

@amrsa1
Copy link
Author

amrsa1 commented Feb 18, 2020

Query params look correct.
There seems to be another cause of issue.(HTTP header ??)

Its really weird, here you are all list of request header

image

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 18, 2020

To see what value is affecting, first request with the same HTTP header.

@amrsa1
Copy link
Author

amrsa1 commented Feb 19, 2020

@H1Gdev
I have tried this on postman and its working fine and im getting fill response according to these additional filters that we are adding,but still curious to know what im missing on frisby it returning only response from the url and ignoring the additional params.

image

it still ignoring the additional params and get the response for the url without aditional params in the code below

    fit('6- Verify by adding aaat last created account in acme API will return status code 200', () => {


        // const url = new URL('https://test.com/api/onboarding/companies/207');
        // url.searchParams.append("additionalParams[include][0]", 'Platforms');
        // url.searchParams.append('additionalParams[include][1]', 'Platforms.PlatformType');
        // url.searchParams.append('additionalParams[include][2]', 'Platforms.PlatformMobileOperator');
        // url.searchParams.append('additionalParams[include][3]', 'Platforms.PlatformSatelliteOperator');
        // url.searchParams.append('additionalParams[include][4]', 'Country');
        // url.searchParams.append('additionalParams[include][5]', 'State');

        // console.log(url.href);
        const url = 'https://test.com/api/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State'
        });
        console.log(url +'?'+ params);
        return frisby
            .setup({
                request: {
                    headers: {
'company-service': 'default',
                    }
                }
            })
            // .get(url +'?'+params)
            .fetch(url +'?', params,{
                method: 'Get'
            })
            .inspectJSON()
            .expect('status',200)

    })

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 19, 2020

First, set all values here and test.

#552 (comment)

By the way, do you know this Web API specifications?

@amrsa1
Copy link
Author

amrsa1 commented Feb 19, 2020

@H1Gdev

i already did that, getting same response , its ignoring the additional params

fit('6- Verify by adding aaaa to last created account in API will return status code 200', () => {

        const url = 'https://test.com/mcapi/onboarding/companies/207';
        const params = new URLSearchParams({
            'additionalParams[include][0]': 'Platforms',
            'additionalParams[include][1]': 'Platforms.PlatformType',
            'additionalParams[include][2]': 'Platforms.PlatformMobileOperator',
            'additionalParams[include][3]': 'Platforms.PlatformSatelliteOperator',
            'additionalParams[include][4]': 'Country',
            'additionalParams[include][5]': 'State'
        });
        console.log(url +'?'+ params);
        return frisby
            .setup({
                request: {
                    redirect: 'manual',
                    headers: {
                        'content-type': 'application/json',
                        'authority': 'ci.test.com',
                        'method': 'GET',
                        'path': '/api/onboarding/companies/207?additionalParams%5Binclude%5D%5B0%5D=Platforms&additionalParams%5Binclude%5D%5B1%5D=Platforms.PlatformType&additionalParams%5Binclude%5D%5B2%5D=Platforms.PlatformMobileOperator&additionalParams%5Binclude%5D%5B3%5D=Platforms.PlatformSatelliteOperator&additionalParams%5Binclude%5D%5B4%5D=Country&additionalParams%5Binclude%5D%5B5%5D=State',
                        'scheme': 'https',
                        'accept': 'application/json, text/plain, */*',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
                        'client-request-id': 'sdfsdf32e',
                       'company-service': 'default',
                       'origin':' https://example.test.com',
                      'referer': 'https://example.com/companies/207',
                      'sec-fetch-dest': 'empty',
                      'sec-fetch-mode': 'cors',             
                      'sec-fetch-site': 'same-site'
                    }
                }
            })
            // .get(url +'?'+params)
            .fetch(url +'?', params,{
                method: 'Get'
            })
            .inspectJSON()
            .expect('status',200)

    })

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 19, 2020

Please delete authority, method, path and scheme.
And add user-agent.

@amrsa1
Copy link
Author

amrsa1 commented Feb 19, 2020

@H1Gdev
Nothing still same

            .setup({
                request: {
                    redirect: 'manual',
                    headers: {
                        'content-type': 'application/json',
                        'accept': 'application/json, text/plain, */*',
                        'accept-encoding': 'gzip, deflate, br',
                        'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
                        // 'client-request-id': 'id_jd23j9jj4',
                        'company-service': 'default',
                        'origin': ' https://example.test.att.com',
                        'referer': 'https://example.test.att.com/companies/207',
                        'sec-fetch-dest': 'empty',
                        'sec-fetch-mode': 'cors',
                        'sec-fetch-site': 'same-site',
                        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
                    }
                }
            })

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 19, 2020

Replace setup values.

    .setup({
      request: {
        redirect: 'manual',
        headers: {
          'content-type': 'application/json',
          'accept': 'application/json, text/plain, */*',
          'accept-encoding': 'gzip, deflate, br',
          'accept-language': 'en-US,en;q=0.9,ar;q=0.8,bs;q=0.7',
          // 'client-request-id': 'id_jd23j9jj4',
          'company-service': 'default',
          'origin': ' https://mc.test.att.com',
          'referer': 'https://mc.test.att.com/companies/207',
          'sec-fetch-dest': 'empty',
          'sec-fetch-mode': 'cors',
          'sec-fetch-site': 'same-site',
          'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
        }
      }
    }, true)

@amrsa1
Copy link
Author

amrsa1 commented Feb 19, 2020

@H1Gdev
it send unauthorized since it seems override the global setup that running before each and containing cookies, so i add the cookies in the header

it returns 200,but again send the regular response and ignoring the additional params

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 19, 2020

I forgot this option...

    .fetch(url.href, {
      method: 'Get',
    }, {
      urlEncode: false,
    })

I think this is issue cause, so I don't think that HTTP header settings are necessary.

@amrsa1
Copy link
Author

amrsa1 commented Feb 20, 2020

YUP, finally!!
it works fine now thanks for you effort

just a quick one, when i should add urlEncode to true or false

@H1Gdev
Copy link
Collaborator

H1Gdev commented Feb 20, 2020

If build URL using URL as in this case, it is already URL-encoded.

In case it is not necessary, so urlEncode is false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants