jpsider / RestPS

Simple ReST Framework for Powershell
MIT License
112 stars 30 forks source link

Is a normal return in the 'body' response? #49

Closed robinmalik closed 3 years ago

robinmalik commented 3 years ago

Hi,

Firstly, thanks for this awesome module.

Unfortunately I've hit an issue trying to use this to respond to a POST request with the following conditions:

A status code of HTTP 200 OK.
A content type of text/plain.
A body that includes the decoded validation token.

This is from https://docs.microsoft.com/en-us/graph/webhooks#notification-endpoint-validation

I've tried parsing the POST args and returning the "validationToken" part only (though it doesn't look like a normal token because it includes descriptive text). Server console output is: 2020-07-25 16:22:57 INFO: Start-RestPSListener: Processing RequestType: POST URL: / Args: validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+f639c33c-aa24-4f37-a065-ded6210a44dd

I've tested a local query to ensure the correct part of the query string is returned and decoded, but when I initiate it from MS, the response moans with Subscription validation request failed. Response must exactly match validationToken query parameter..

The full server response is this:

2020-07-25 16:34:43 TRACE: Start-RestPSListener: Determining VerificationType: ''
2020-07-25 16:34:43 TRACE: Start-RestPSListener: NOT Executing Invoke-ValidateClient
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Executing Invoke-GetBody
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Determining Method and URL
2020-07-25 16:34:43 INFO: Start-RestPSListener: New Request - Method: POST URL: /?validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+f82d8181-f84d-4ed5-8dcb-850bf3244bb6
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Processing Request, Checking for Shutdown Command
2020-07-25 16:34:43 INFO: Start-RestPSListener: Processing RequestType: POST URL: / Args: validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+f82d8181-f84d-4ed5-8dcb-850bf3244bb6
2020-07-25 16:34:43 INFO: Start-RestPSListener: Finished request. StatusCode: 200 StatusDesc: OK
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Streaming response back to requestor.
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Streaming response is complete.
2020-07-25 16:34:43 TRACE: Start-RestPSListener: Captured incoming request

Any ideas?

Thanks!

robinmalik commented 3 years ago

Performing the test locally with Invoke-WebRequest and I'm seeing it's being returned in quotes:

HTTP/1.1 200 OK Content-Length: 119 Content-Type: application/json Date: Sat, 25 Jul 2020 15:37:55 GMT Server: Microsoft-HTTPAPI/2.0

"Validation: Testing client application reachability for subscription Request-Id: 7df11aaa-75a8-41b2-8a61-4dc86cd9751b"

Perhaps that's the problem?

jpsider commented 3 years ago

So you are getting back more than expected? (Sorry on my phone right now)

If so, are you writing the value out in the script you are executing? If so trim it down to just the value you expect, see if that works, you may need to Add Out-Null to the end of the logging line.

robinmalik commented 3 years ago

No worries, please take your time!

I've found the culprits - would have helped if I'd read the headers of the IWR too. Using a couple of POSTMAN tests by Microsoft it's showing it failing due to both the content type and the return body.

image

Do we have a way to modify this with native functionality in the module or would it require some substantial modification? I appreciate this is a REST module so returning application/json is 'as it should be' :)

jpsider commented 3 years ago

I may be confused. In the module I believe I attempt to set it to Json. Before sending the response back. I'd have to take a closer look.

I'm open to any changes to the module, that's why it's open source! It should ideally work for everyone!

jpsider commented 3 years ago

$script:Response.ContentType = 'application/json'

That's hard coded in the 'invoke-StreamOutput' function

robinmalik commented 3 years ago

It does set it to JSON. A quick and dirty hack of Invoke-StreamOutput to remove that and I managed to get it working, though working it into the module itself will be a bit of work. I'll have a think! I use Gitlab all the time but I think this would be my second or third time trying to propose changes here. Will re-familiarise myself with the process :)

jpsider commented 3 years ago

Sounds good looking forward to see what you come up with.

jpsider commented 3 years ago

The updated module has been pushed to the Gallery! https://www.powershellgallery.com/packages/RestPS/7.0.32 - Let me know how it goes.

robinmalik commented 3 years ago

Thanks! I'll test it soon.