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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Error 308 Permanent Redirect in X++ code

(0) ShareShare
ReportReport
Posted on by 2,176

Hi,

I need to send an API request to SMS API.

My Python code is working but my X code is not working.

Here's my Python code:

import requests

headers = {"Accept":"application/json"}

data = {"AppSid":"#############YxLtXtaN###",
        "SenderID":"####23423#####",
        "Body":"This is a test message.",
        "Recipient":"###45645######",
        "responseType":"JSON",
        "CorrelationID":"",
        "baseEncode":"true",
        "statusCallback":"sent",
        "async":"false"}

r = requests.post('http://myapi/rest/SMS/messages', auth=('user@domain.com', 'password'), headers=headers,
                data=data)

Here's my X /C# code:

class Class1
{
    public static void main(Args _args)
    {
        str destinationUrl = 'myapi', requestXml, responseXml;
        System.Net.HttpWebRequest    request;
        System.Net.HttpWebResponse   response;
        CLRObject            clrObj;
        System.Byte[]          bytes;
        System.Text.Encoding      utf8;
        System.IO.Stream        requestStream, responseStream;
        System.IO.StreamReader     streamReader;
        System.Exception        ex;
        System.Net.WebHeaderCollection httpHeader;
        str               byteStr;
        System.Byte[]          byteArray;
        System.IO.Stream        stream;
        System.IO.Stream        dataStream;
        byteStr = strfmt('%1:%2', "user@domain.com", "password");
        requestXml = " {\"AppSid\":\"###########\", \"SenderID\":\"########-AD\", \"Body\":\"This is a test message from ## from X   Coding Language..\", \"Recipient\":\"######\", \"responseType\":\"JSON\", \"CorrelationID\":\"\", \"baseEncode\":\"true\", \"statusCallback\":\"sent\", \"async\":\"false\"}";
        try
        {
            new InteropPermission(InteropKind::ClrInterop).assert();
            httpHeader = new System.Net.WebHeaderCollection();
            clrObj = System.Net.WebRequest::Create(destinationUrl);
            request = clrObj;
            utf8 = System.Text.Encoding::get_UTF8();
            bytes = utf8.GetBytes(requestXml);
            request.set_KeepAlive(true);
            request.set_ContentType("application/xml");
            request.AllowAutoRedirect=true;
            utf8    = System.Text.Encoding::get_UTF8();
            byteArray  = utf8.GetBytes(byteStr);
            byteStr   = System.Convert::ToBase64String(byteArray);
            httpHeader.Add("Authorization", 'Basic '   byteStr);
            request.set_ContentType("text/xml; encoding='utf-8'");
            request.set_ContentLength(bytes.get_Length());
            request.set_Method("POST");
            request.set_Headers(httpHeader);
            requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.get_Length());
            response = request.GetResponse();
            responseStream = response.GetResponseStream();
            streamReader = new System.IO.StreamReader(responseStream);
            responseXml = streamReader.ReadToEnd();
            info(responseXml);
        }
        catch (Exception::CLRError)
        {
            //bp deviation documented
            ex = CLRInterop::getLastException().GetBaseException();
            error(ex.get_Message());
        }
        requestStream.Close();
        streamReader.Close();
        responseStream.Close();
        response.Close();
    }

}

I'm getting this error:

error code : The remote server returned an error: (308) Permanent Redirect.

I have the same question (0)
  • Verified answer
    MYGz Profile Picture
    2,176 on at

    This is resolved.

    It was redirecting it to HTTPS which was not getting auto handled in X++. And credentials were not needed which was being ignored by Python and Postman but were not ignored in X++ due to which it was trying those credentials and failing. So removed the credentials part. These 2 fixed the code.

  • huijij Profile Picture
    19,811 on at

    Thank you for your contribution to the forum.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 434 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans