'Error 308 Permanent Redirect in X++/C# code

I need to send an API request to SMS API.

My Python code is working but my X++/C# code is not working.

I tried with Postman, it is working as well.

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=('[email protected]', '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', "[email protected]", "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.



Solution 1:[1]

I made two changes.

  1. removed the Authorization by commenting it out //httpHeader.Add("Authorization", 'Basic ' + byteStr);

  2. Made it HTTPS instead of HTTP. I could see the Location: HTTPS in the exception Response object in Visual Studio.

These 2 things resolved the issue for me.

Solution 2:[2]

I don't know about x++, but you could try explicitly allowing redirect on your webRequest object.

webRequest = System.Net.WebRequest::Create('http://myapi/rest/SMS/messages') as System.Net.HttpWebRequest;


webRequest.AllowAutoRedirect = true;
webRequest.MaximumAutomaticRedirections = 1;  //Set value according to your requirements

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Yusuf
Solution 2 jason.kaisersmith