Al: Could not attach cookies with HttpClient object

Created on 29 Mar 2019  路  7Comments  路  Source: microsoft/AL

Describe the bug
Is not posible to make HttpRequest that need to attach a Cookie. I'm trying to make some web service calls that need to attach a cookie with the Http Post call. As HttpClient object has no any property to access to the request cookies, I'm passing the cookie as the header 'Cookie', but It's not working as expected. I tested the same use case with Postman and Python and when you attach the 'Cookie' header you get the same result that if you attach a Cookie.

I tested it with a mock server and the header 'Cookie' was sent but not the Cookie part.

To Reproduce
I attach an screenshot with a sample code in AL.

Expected behavior
The 'Cookie' header should has the same efect as attach a Cookie with the request.

Screenshots
Thats my code:
image

The same use case work if you test it with Postman.
get_request_digest_postman

Versions:

  • AL Langauge: 2.1.109895
  • Business Central: 13.0.26198

Thanks in advance

bug

Most helpful comment

Hi @clagoa ,
I have run across the same issue now.
The root cause is likely in the underlying HttpClient in .NET. The following links explain that Cookie header is overriden by default from HttpClientHandler:

Unfortunately we do not have the HttpClientHandler in our hands in BC and so we have no way of using HttpClient with cookies from AL without using some .NET or Azure Functions or former Web Request and similar libraries.

@thpeder - HttpClient actually actively throws our cookies headers away (see those links above). If there can be done anything on AL HttpClient implementation side to make ALL the headers pass it would help us.

Thanks!

All 7 comments

Hi @clagoa,
The httpclient doesn't provide any extra help for working with cookies, so you will need to read the spec for how cookies are written in the headers.
If you want this feature in a later version then please go to Ideas with your request.

Hello @thpeder ,

Maybe I did not explain myself well, but writing cookies as a HTTP Header is exactly what I'm trying to do. I'm passing the cookies as the 'Cookie' header following the rfc6265 spec.

To give you more context, what I'm trying to do is to upload a file to Sharepoint, and to authenticate, I need to complete 3 steps:

  • Get the Security Token which is an HTTP POST call and I get it done without problems
  • Get the Access Cookies which is another HTTP POST call and I also get it correctly. The cookies are extracted from the 'Set-Cookie' header sent by the server in the response
  • Obtain the Request Digest which is an HTTP POST call that need to attach the cookies received in the previous step.

It is in this last step where I have the problem. I'm including the cookies that I must pass in the request as the 'Cookie' header, but the authentication fails exactly as if no header was sent.

I can complete this auth sequence in different environments (python, postman and javascript) and the procedure is always the same and works correctly. But it does not work from BC.

Can you confirm me that HttpClient object in BC supports the 'Cookie' header?

Hi @clagoa ,
I have run across the same issue now.
The root cause is likely in the underlying HttpClient in .NET. The following links explain that Cookie header is overriden by default from HttpClientHandler:

Unfortunately we do not have the HttpClientHandler in our hands in BC and so we have no way of using HttpClient with cookies from AL without using some .NET or Azure Functions or former Web Request and similar libraries.

@thpeder - HttpClient actually actively throws our cookies headers away (see those links above). If there can be done anything on AL HttpClient implementation side to make ALL the headers pass it would help us.

Thanks!

Hi @ichladil,

Thanks for delving into this topic. I hope the BC team could give some solution to our needs soon, there are many Web Services that need the use of cookies.

Hi,
I haven't investigated what can be done to keep all the cookie headers as well, but we have cookie support on our backlog, but to be honest we have some other features that are higher on the list at the moment, so I do not know when support for it will be implemented.
I would advice you to add it to Ideas since it is easier for us to know where to invest our effort by looking there.

We are being impacted by this as well. We don't need cookie handling, per se. We just need to be able to set the "Cookie" HTTP header. @thpeder could you please re-open this issue? Again, we just need the HttpClient to not discard the Cookie header. We can handle the cookies ourselves. Some web services require a Cookie header value and we are not able to send it.

Hi @atoader,
I added the Idea.

Was this page helpful?
0 / 5 - 0 ratings