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

Raw response promises #141

Closed
luisrpfigueiredo opened this issue Feb 8, 2019 · 10 comments
Closed

Raw response promises #141

luisrpfigueiredo opened this issue Feb 8, 2019 · 10 comments

Comments

@luisrpfigueiredo
Copy link

luisrpfigueiredo commented Feb 8, 2019

When accessing the graph API response, we can pass a third option to the callback function in order to be able get a raw response, granting access to the headers.

Is there a way to do so through promises? If not, it would be nice to have, as using callbacks is... painful. A potential solution would be to have a .setResponseType('raw') or similar option available.

Edit: I found the .responseType functionality. Passing in MicrosoftGraph.ResponseType.RAW, or the hardcoded string "raw" is not working. Has this been implemented yet? ResponseType.RAW is not defined.

For reference, I'm trying to get the location header of the graph response after accessing the /items/{itemId}/copy endpoint. I know the request is successful because the items are being copied, just the response is always undefined.

@muthurathinam
Copy link
Contributor

@luisrpfigueiredo
Getting rawResponse through promises is implemented and merged to the release branch and same will be available in next release.

Refer: Getting Raw Response

@luis-figueiredo
Copy link

Thank you for the swift reply. Is there any estimate on when this release might be?

@muthurathinam
Copy link
Contributor

I don't have the exact ETA for this, as we have more stuffs lined up before next release. I can assure you that before end of this month.

@muthurathinam
Copy link
Contributor

Fixed in v1.5.2.

@dancampbell691
Copy link

@luisrpfigueiredo - did you get this working with 1.5.2 or later? I'm on 1.6 and I still get an empty response.headers.
I've tried using both the callback method with (resp, err, rawResponse) and the promise method with .responseType(ResponseType.RAW), and both beta and v1.0, but in all cases I get nothing in the headers array. If I look at the network tab in Chrome, I can see that the response headers did come back, including the Location value that I need. But I can't figure out how to get it returned from the GraphClient call.

@muthurathinam
Copy link
Contributor

Hi @dancampbell691 Can you share me the code that you have used to make a request ?

@dancampbell691
Copy link

hi @muthurathinam. Here's the promise-based version. This successfully returns the raw response which includes the 202, but the headers array of the response object is empty. If I don't include the .responseType part, then the response is just an empty string. As I noted above, Chrome Network tab shows the response headers including location.
One more thing - I had to punt on this and just made the call directly with axios (i.e. without the client sdk), and it worked as expected.

res = await graphClient
    .api(`drives/${this.driveId}/items/${this.attachment.FileId}/copy`)
    .responseType(ResponseType.RAW)
    .version('beta')
    .post( 
    {
        "parentReference": { 
            'driveId': this.destinationDriveId,
            'id': this.destinationStoreGraphProperties.id
            }
        , 'name': `test ingest doc${this.$moment().format('HH-m-s')}.pdf` // temp add time so no name conflict
    });
console.log(res);

Any ideas would be appreciated. Thanks!

@muthurathinam
Copy link
Contributor

@dancampbell691 The returned raw response is of this type. So when you do console.log the headers and body won't get printed along with the response object. you have to use one of the body methods to read the body and headers methods to work with headers.

Basically, you have to use res.headers.get("location") in the above snippet, to get location header from the raw response

@dancampbell691
Copy link

@muthurathinam - Thanks! res.headers.get('location') did the trick.

It would be helpful if there were an example in the documentation that showed the entire flow of a copy, both the client call for the copy itself, then getting the location from the response, and calling the location url.

@muthurathinam
Copy link
Contributor

@dancampbell691 Sounds valid, let me update the doc showing a smiliar flow which helps in working with raw response. 😊

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

4 participants