web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Send data to external API - setting credentials on the header

(0) ShareShare
ReportReport
Posted on by 471

Hi All,

I am trying to send the data as json to external api and for that I am trying to set credentials on header but getting 401 error as response uaing below code 

httpHeader.Add("Authorization: API Key keyname keyvalue") also attached the postman image keyname and key value are set to the blacked out values.

Can you please let me know how to fix this?

try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        httpHeader = new System.Net.WebHeaderCollection();
        request = System.Net.WebRequest::Create(_url);
        utf8 = System.Text.Encoding::get_UTF8();
        bytes = utf8.GetBytes(_json);
        request.set_Method(_method);
        httpHeader.Add("Authorization: API Key keyname keyvalue");
        request.set_Headers(httpHeader);
        request.ContentType = 'application/json';
        request.set_ContentLength(bytes.get_Length());
        requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.get_Length());
        response = request.GetResponse();
        responseStream = response.GetResponseStream();
        streamReader = new System.IO.StreamReader(responseStream);
        responseJson = streamReader.ReadToEnd();
        info(responseJson);
    }

    catch (Exception::CLRError)
    {
        ex = CLRInterop::getLastException().GetBaseException();
        error(ex.get_Message());
    }

    requestStream.Close();
    streamReader.Close();
    responseStream.Close();

    response.Close()

postman.jpg

Thanks

  • Martin Dráb Profile Picture
    240,852 Most Valuable Professional on at

    What you should send depends on what the web service expects, which we don't know.

    By the way, just for inspiration, let me show you a few things that can simplify your code:

    using System.IO;
    using new System.Net;
    
    System.Exception ex;
    
    try
    {
    	
    	request = WebRequest::Create(_url);
    	request.set_Method(_method);
    	
    	httpHeader = new WebHeaderCollection();
    	httpHeader.Add("Authorization: API Key keyname keyvalue");
    	request.Headers = httpHeader;
    	request.ContentType = 'application/json';
    	
    	utf8 = System.Text.Encoding::get_UTF8();
    	bytes = utf8.GetBytes(_json);
    	request.ContentLength = bytes.Length;
    	
    	using (Stream requestStream = request.GetRequestStream())
    	{
    		requestStream.Write(bytes, 0, bytes.Length);
    		using (WebResponse response = request.GetResponse())
    		{
    			using (Stream responseStream = response.GetResponseStream())
    			{
    				using (StreamReader streamReader = new StreamReader(responseStream);
    				{
    					responseJson = streamReader.ReadToEnd();
    				}
    			}
    		}
    	}
    	
    	info(responseJson);
    }
    catch (ex)
    {
    	error(ex.Message);
    }

  • Dev Profile Picture
    471 on at

    Hi Martin,

    I need to know how to set the credentials, I have the json that I need to send but I am getting authentication error 401.

  • Dev Profile Picture
    471 on at

    Do we need to send all these details to header too?

    Screenshot-2023_2D00_01_2D00_13-182216.jpg

  • Martin Dráb Profile Picture
    240,852 Most Valuable Professional on at

    I'm sorry, but I know nothing about the API you're using, so I can't tell you how you need to communicate with it. If you know that it's using some standard mechanism for specifying API keys, share the information with us.

    If you don't know or it's using a custom solution, consult documentation of the API or contact the provider.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 523 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 408 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 354

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans