locked
How to access data from a rest api URL for which credentials are required? RRS feed

  • Question

  • We have a rest api URL.

    Ou requirement is to access the data from the rest url and show the data on aspx page

    The REST URL is from different platform and have different authentication.

    How to pass credentials along with the REST URL?

    Code snippets will be useful.

    Thanks


    VINAYAK BHANGALE

    Thursday, July 19, 2018 7:12 AM

All replies

  • Hi

    try this

    # Eg. User name="admin", Password="admin" for this code sample.
    $user = "username"
    $pass = "password"

    # Build auth header
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

    # Set proper headers
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
    $headers.Add('Accept','application/json')


    # Specify endpoint uri
    $uri = "Your Rest UTL"

    # Specify HTTP method
    $method = "get"


    {request.body ? "$body = \"" :""}}

    # Send HTTP request
    $response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri

    # Print response
    $response.RawContent

    • Proposed as answer by Sara Fan Friday, July 20, 2018 2:05 AM
    Thursday, July 19, 2018 2:55 PM
  • Hi Vinayak Bhangale,

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best regards,

    Sara Fan

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, July 20, 2018 2:06 AM